﻿Imports System.IO
Imports System.IO.File
Imports Cognex.VisionPro.ToolGroup
Imports Cognex.VisionPro.PMAlign
Imports Cognex.VisionPro.CalibFix
Imports Cognex.VisionPro
Imports Cognex.VisionPro.ImageProcessing
Imports System.Math


Public Class FrmAddProd2
    Dim TextTorq(30) As System.Windows.Forms.TextBox
    Dim TextAngle(30) As System.Windows.Forms.TextBox
    Dim TextFeeder(30) As System.Windows.Forms.TextBox
    Dim f() As String
    Dim ToolPath As String = ""
    Dim pointNO As Integer = 0
    Dim CADPoint(32) As MITSUBISHI_Point
    Dim CADPointName(32) As String
    Dim flag(7) As Boolean

    Dim tmpfifo1 As CogAcqFifoTool
    Dim tmpfifo2 As CogAcqFifoTool
    Dim tmpfifo3 As CogAcqFifoTool
    Dim tmpPMAlign1 As CogPMAlignTool
    Dim tmpPMAlign2 As CogPMAlignTool
    Dim tmpPMAlign3 As CogPMAlignTool

    Dim tmpCalibCB1 As CogCalibCheckerboardTool
    Dim tmpCalibCB2 As CogCalibCheckerboardTool
    Dim tmpCalibCB3 As CogCalibCheckerboardTool

    Dim prePhoto As MITSUBISHI_Point

    Dim myImageSharp As CogImageSharpnessTool
    Dim sharpScore As Double
    Dim myFrameGrabbers As CogFrameGrabbers
    Dim myFrameGrabber As ICogFrameGrabber
    Dim myAcqFifo As ICogAcqFifo
    Dim sharp_NO As String
    '
    Dim TrainRate(2) As Double
    Dim trainAngle(2) As Double
    Dim HomeX(2) As Double
    Dim HomeY(2) As Double

    Dim Rate0 As Double = 511 / 659 '图像与现实框比例

    Dim RateZoom As Double = 1  '显示1 缩放
    ''  Dim RateZoom As Double = 1  '显示1 缩放
    '  Dim RateZoom As Double = 1 '显示2 缩放
    Dim RECTX1 As Double '起点X
    Dim RECTY1 As Double '起点Y
    Dim RECTX2 As Double '终点X
    Dim RECTY2 As Double '终点Y
    Dim GetRegion1 As Boolean = False
    Dim MoveRegion1 As Boolean = False
    Dim GetRegion2 As Boolean = False
    Dim MoveRegion2 As Boolean = False
    Dim RgR As Double
    Dim RgSideX As Double
    Dim RgSideY As Double
    Dim PhotoSU As Integer

    Private Sub FrmAddProd_FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing
        Timer1.Enabled = False

        AddProdflag = False
        Dim Re_date As String

        StepNO = "STEP5"
        Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
        'Re_date = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")
        'Re_date = SendDataAndRecvice(StepNO, "192.168.1.120", "2016")


        Me.Dispose()
    End Sub

    Private Sub FrmAddProd_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
        Motor_flag = True
        Button_SVON.BackColor = Color.YellowGreen
        Button_SVOFF.BackColor = Color.WhiteSmoke

        Me.Size = New System.Drawing.Size(835, 529)
        InitControl() '初始化控件
        Button_NextStep.Enabled = True


        ToolPath = DebugPath & "\tool"

        GetDirectFiles(ToolPath) '获取产品名
        GroupBox1.Visible = False

        For I = 0 To 31
            CADPoint(I).X = 0
            CADPoint(I).Y = 0
            CADPoint(I).Z = 0
            CADPoint(I).C = 0
        Next
        CADPointName(0) = "photo1cad"
        Panel4.Visible = False
        AddProdflag = True

        Button_PrePoint.Enabled = False
        For I = 0 To 7
            flag(I) = False
        Next
        TabControl1.SelectedIndex = 0

        ComboBox_Tool.SelectedItem = 1

        ComboBox_OldProd.Focus()
        StepOK1 = False
        StepOK2 = False
        StepOK3 = False
        Try
            Dim bState As Boolean
            Dim lCnt As Integer
            Dim sRobotInf As Object = New Char()
            Dim sRobotID As Object = New Char()
            Dim sName As Object = New Char()
            Dim sData As Object = New Char()
            Dim I As Integer

         
            'lRobotID = Val(sData)

   
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try


    End Sub

    ''' <summary>
    ''' 获取目录下文件
    ''' </summary>
    ''' <param name="s"></param>
    ''' <remarks></remarks>
    Private Sub GetDirectFiles(ByVal s As String)
        '找出位于 s 根目录下的所有文件
        Try
            Dim i As Integer = 0
            Dim t As Collections.ObjectModel.ReadOnlyCollection(Of String) = My.Computer.FileSystem.GetFiles(s), k As String, name() As String
            ComboBox_OldProd.Items.Clear()
            For Each k In My.Computer.FileSystem.GetDirectories(s)
                ReDim Preserve f(i)
                f(i) = k
                Dim myFileInfo As New IO.FileInfo(f(i))
                name = Split(f(i), "\")
                ComboBox_OldProd.Items.Add(name(UBound(name))) '获取文件名
                i += 1
            Next
            If i > 0 Then
                ComboBox_OldProd.SelectedIndex = 0
            End If
        Catch

        End Try
    End Sub

    ''' <summary>
    ''' 上一步按钮事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_PreStep_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_PreStep.Click

        If TabControl1.SelectedIndex > 0 Then
            TabControl1.SelectedIndex = TabControl1.SelectedIndex - 1
            If TabControl1.SelectedIndex = 0 Then
                Button_PreStep.Enabled = False
                Button_NextStep.Enabled = True
            Else
                Button_PreStep.Enabled = True
            End If
        End If
        Select Case TabControl1.SelectedIndex
            Case 0  '保存产品信息
                Label_ProdInfo.BackColor = Color.Yellow  'step1
                Label_ModelSet1.BackColor = Color.WhiteSmoke 'step2
                Me.Size = New System.Drawing.Size(835, 529)
                GroupBox1.Visible = False
            Case 1   '做模板1
                Label_ModelSet1.BackColor = Color.Yellow 'step2
                Label_ModelSet2.BackColor = Color.WhiteSmoke 'step3
                Me.Size = New System.Drawing.Size(835, 715)
                GroupBox1.Visible = True
            Case 2 '做模板2
                Label_ModelSet2.BackColor = Color.Yellow 'step2
                Label_PointWrite.BackColor = Color.WhiteSmoke 'step3
                Me.Size = New System.Drawing.Size(835, 715)
                GroupBox1.Visible = True
            Case 3  '示教点位
                Label_PointWrite.BackColor = Color.Yellow 'step3
                Label_PointPara.BackColor = Color.WhiteSmoke 'step4
                Me.Size = New System.Drawing.Size(835, 715)
                GroupBox1.Visible = True
            Case 4 '保存螺丝点信息
                Label_PointPara.BackColor = Color.Yellow 'step4
                Label_Complete.BackColor = Color.WhiteSmoke 'step5
                Me.Size = New System.Drawing.Size(835, 529)
                GroupBox1.Visible = False
        End Select

    End Sub

    ''' <summary>
    ''' 下一步按钮事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_NextStep_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_NextStep.Click
        SaveData(TabControl1.SelectedIndex)
    End Sub

    ''' <summary>
    ''' 保存数据
    ''' </summary>
    ''' <param name="page"></param>
    ''' <remarks></remarks>
    Private Sub SaveData(ByVal page As Integer)
        Dim paste_flag As Boolean
        paste_flag = True
        Select Case page
            Case 0  '保存产品信息
                Label_ProdInfo.BackColor = Color.Lime 'step2
                Label_ModelSet1.BackColor = Color.Yellow 'step3
                '复制产品，并获取检测工具数组
                If ComboBox_OldProd.Text = "" Then
                    MsgBox("相似产品未指定！")
                    GoTo d
                End If
                If TextBox_NewProdName.Text = "" Then
                    MsgBox("新产品名未指定！")
                    GoTo d
                End If
                If TextBox_PointCount.Text = "" Then
                    MsgBox("锁附螺丝颗数未指定！")
                    GoTo d
                End If

                ' GetDirectFiles(ToolPath) '获取产品名

                For i = 0 To ComboBox_OldProd.Items.Count - 1
                    If ComboBox_OldProd.Items(i).ToString = TextBox_NewProdName.Text Then
                        Select Case MsgBox("已存在产品" & ComboBox_OldProd.Items(i).ToString & "，是否修改？", MsgBoxStyle.YesNo)
                            Case MsgBoxResult.Yes
                                '确定要执行的
                                paste_flag = False
                            Case MsgBoxResult.No
                                '取消要执行的
                                GoTo d
                        End Select
                    End If
                Next

                Try
                    '复制机种
                    If paste_flag = True Then
                        Dim name As String = TextBox_NewProdName.Text
                        Dim path As String = ToolPath & "\" & ComboBox_OldProd.Text '& "\"
                        If Dir(path, FileAttribute.Directory) <> "" Then
                            My.Computer.FileSystem.CopyDirectory(path, ToolPath & "\" & name)
                            CopyMysql(ComboBox_OldProd.Text, name)
                        End If
                    End If

                    '保存螺丝颗数和螺丝型号
                    Dim filepath = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\"
                    Dim Para_num As Integer = FreeFile()
                    'Dim OpenParameter As Boolean = Exists(filepath & "Parameter.txt")
                    Dim PointCount As Integer = Val(TextBox_PointCount.Text)
                    Dim ScrewSN As String = TextBox_ScrewSN.Text
                    Dim NEWScrewPara As ScrewPara
                    NEWScrewPara.PointCount = PointCount
                    NEWScrewPara.ScrewSN = ScrewSN

                    FileOpen(Para_num, filepath & "Parameter.txt", OpenMode.Random)
                    FilePut(Para_num, NEWScrewPara)
                    FileClose(Para_num)
                    'flag(2) = True
                    FrmMain.TCmbPro.Text = TextBox_NewProdName.Text
                    'If flag(2) = False Then
                    '    Button_NextStep.Enabled = False
                    'Else
                    '    Button_NextStep.Enabled = True
                    'End If

                    '保存延时
                    If MsgBox("保存停止延时？", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                        Dim sql1 As String = ""                  
                        Dim Tvalue As Double = Val(TextBox_MotorDly.Text)
                        sql1 = " update " & " TimerDelay " & "set TimeSet=" & Tvalue & " where TimerNO=" & "'Timer1' and Product='" & TextBox_NewProdName.Text & "'"
                        HandleMysql(sql1)
                    End If
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
                '加载相机
                ' LoadCameraTool()

                '指定机器人工具(0,1,2,3,4,5)
                ComboBox_Tool.SelectedIndex = 1
                '指定机器人速度20（）
                ComboBox_speed.SelectedIndex = 8
                '指定机器人寸动距离
                ComboBox_stepType.SelectedIndex = 3
                Me.Size = New System.Drawing.Size(955, 715)
                GroupBox1.Visible = True


            Case 1   '做模板1
                '加载相机工具
                Label_ModelSet1.BackColor = Color.Lime 'step2
                Label_ModelSet2.BackColor = Color.Yellow 'step3
                If flag(5) = False Then
                    MsgBox("模板1数据未保存！")
                End If

            Case 2   '做模板2
                Label_ModelSet2.BackColor = Color.Lime 'step2
                Label_ModelSet3.BackColor = Color.Yellow
                If flag(6) = False Then
                    MsgBox("模板2数据未保存！")
                End If

                'ComboBox_Tool.SelectedIndex = 2
            Case 3  '做模板3
                Label_ModelSet3.BackColor = Color.Lime 'step3
                Label_PointWrite.BackColor = Color.Yellow 'step4
                If flag(7) = False Then
                    MsgBox("模板3数据未保存！")
                End If

                'ComboBox_Tool.SelectedIndex = 2
            Case 4  '示教点位
                Label_PointWrite.BackColor = Color.Lime 'step4
                Label_PointPara.BackColor = Color.Yellow 'step5
                pointNO = 0
                CADPointName(pointNO) = "photo1cad"
                TextBox_pointName.Text = CADPointName(pointNO)
                Me.Size = New System.Drawing.Size(835, 529)
                GroupBox1.Visible = False


            Case 5 '保存螺丝点信息
                Label_PointPara.BackColor = Color.Lime
                Label_Complete.BackColor = Color.Lime 'step5
                SavePointPara()
                Button_NextStep.Enabled = False
        End Select
        If TabControl1.SelectedIndex < TabControl1.TabCount Then
            TabControl1.SelectedIndex = TabControl1.SelectedIndex + 1
            Button_PreStep.Enabled = True
        End If
d:
    End Sub

    ''' <summary>
    ''' 保存set01~30点位数据
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub SavePointPara()
        Dim i As Integer = 0
        Try
            Dim sql1 As String = ""
            Dim DBpath As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\PointPara.accdb"
            If File.Exists(DBpath) = False Then
                MsgBox("文件不存在：" & DBpath)
                GoTo d
            End If
            Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DBpath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
            For i = 1 To 30
                If i < 10 Then
                    sql1 = " update " & " PointPara " & "set torque=" & TextTorq(i).Text & ",angle=" & TextAngle(i).Text & ",feeder=" & TextFeeder(i).Text & ",disable=0" & " where PointName=" & "'set0" & i & "' and Product='" & TextBox_NewProdName.Text & "'"
                Else
                    sql1 = " update " & " PointPara " & "set torque=" & TextTorq(i).Text & ",angle=" & TextAngle(i).Text & ",feeder=" & TextFeeder(i).Text & ",disable=0" & " where PointName=" & "'set" & i & "' and Product='" & TextBox_NewProdName.Text & "'"
                End If
                HandleMysql(sql1)
            Next
            flag(4) = True
            MsgBox("点位参数保存成功！")
d:
        Catch ex As Exception
            MsgBox("SavePointPara err:" & ex.Message.ToString)
        End Try
    End Sub

    ''' <summary>
    ''' 初始化控件
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub InitControl()
        Label_ProdInfo.BackColor = Color.Yellow 'step1
        Label_ModelSet1.BackColor = Color.WhiteSmoke 'step2
        Label_ModelSet2.BackColor = Color.WhiteSmoke 'step3
        Label_ModelSet3.BackColor = Color.WhiteSmoke 'step4
        Label_PointWrite.BackColor = Color.WhiteSmoke 'step5
        Label_PointPara.BackColor = Color.WhiteSmoke 'step6
        Label_Complete.BackColor = Color.WhiteSmoke 'step7

        TextTorq(0) = TextBox_TorqAll
        TextAngle(0) = TextBox_AngleAll
        TextFeeder(0) = TextBox_FeederAll

        TextTorq(1) = TextBox_Torq1
        TextAngle(1) = TextBox_Angle1
        TextFeeder(1) = TextBox_feeder1

        TextTorq(2) = TextBox_Torq2
        TextAngle(2) = TextBox_Angle2
        TextFeeder(2) = TextBox_feeder2

        TextTorq(3) = TextBox_Torq3
        TextAngle(3) = TextBox_Angle3
        TextFeeder(3) = TextBox_feeder3

        TextTorq(4) = TextBox_Torq4
        TextAngle(4) = TextBox_Angle4
        TextFeeder(4) = TextBox_feeder4

        TextTorq(5) = TextBox_Torq5
        TextAngle(5) = TextBox_Angle5
        TextFeeder(5) = TextBox_feeder5

        TextTorq(6) = TextBox_Torq6
        TextAngle(6) = TextBox_Angle6
        TextFeeder(6) = TextBox_feeder6

        TextTorq(7) = TextBox_Torq7
        TextAngle(7) = TextBox_Angle7
        TextFeeder(7) = TextBox_feeder7

        TextTorq(8) = TextBox_Torq8
        TextAngle(8) = TextBox_Angle8
        TextFeeder(8) = TextBox_feeder8

        TextTorq(9) = TextBox_Torq9
        TextAngle(9) = TextBox_Angle9
        TextFeeder(9) = TextBox_feeder9

        TextTorq(10) = TextBox_Torq10
        TextAngle(10) = TextBox_Angle10
        TextFeeder(10) = TextBox_feeder10

        TextTorq(11) = TextBox_Torq11
        TextAngle(11) = TextBox_Angle11
        TextFeeder(11) = TextBox_feeder11

        TextTorq(12) = TextBox_Torq12
        TextAngle(12) = TextBox_Angle12
        TextFeeder(12) = TextBox_feeder12

        TextTorq(13) = TextBox_Torq13
        TextAngle(13) = TextBox_Angle13
        TextFeeder(13) = TextBox_feeder13

        TextTorq(14) = TextBox_Torq14
        TextAngle(14) = TextBox_Angle14
        TextFeeder(14) = TextBox_feeder14

        TextTorq(15) = TextBox_Torq15
        TextAngle(15) = TextBox_Angle15
        TextFeeder(15) = TextBox_feeder15

        TextTorq(16) = TextBox_Torq16
        TextAngle(16) = TextBox_Angle16
        TextFeeder(16) = TextBox_feeder16

        TextTorq(17) = TextBox_Torq17
        TextAngle(17) = TextBox_Angle17
        TextFeeder(17) = TextBox_feeder17

        TextTorq(18) = TextBox_Torq18
        TextAngle(18) = TextBox_Angle18
        TextFeeder(18) = TextBox_feeder18

        TextTorq(19) = TextBox_Torq19
        TextAngle(19) = TextBox_Angle19
        TextFeeder(19) = TextBox_feeder19

        TextTorq(20) = TextBox_Torq20
        TextAngle(20) = TextBox_Angle20
        TextFeeder(20) = TextBox_feeder20

        TextTorq(21) = TextBox_Torq21
        TextAngle(21) = TextBox_Angle21
        TextFeeder(21) = TextBox_feeder21

        TextTorq(22) = TextBox_Torq22
        TextAngle(22) = TextBox_Angle22
        TextFeeder(22) = TextBox_feeder22

        TextTorq(23) = TextBox_Torq23
        TextAngle(23) = TextBox_Angle23
        TextFeeder(23) = TextBox_feeder23

        TextTorq(24) = TextBox_Torq24
        TextAngle(24) = TextBox_Angle24
        TextFeeder(24) = TextBox_feeder24

        TextTorq(25) = TextBox_Torq25
        TextAngle(25) = TextBox_Angle25
        TextFeeder(25) = TextBox_feeder25

        TextTorq(26) = TextBox_Torq26
        TextAngle(26) = TextBox_Angle26
        TextFeeder(26) = TextBox_feeder26

        TextTorq(27) = TextBox_Torq27
        TextAngle(27) = TextBox_Angle27
        TextFeeder(27) = TextBox_feeder27

        TextTorq(28) = TextBox_Torq28
        TextAngle(28) = TextBox_Angle28
        TextFeeder(28) = TextBox_feeder28

        TextTorq(29) = TextBox_Torq29
        TextAngle(29) = TextBox_Angle29
        TextFeeder(29) = TextBox_feeder29

        TextTorq(30) = TextBox_Torq30
        TextAngle(30) = TextBox_Angle30
        TextFeeder(30) = TextBox_feeder30
        For i = 0 To 30
            TextTorq(i).Text = "0"
            TextAngle(i).Text = "0"
            TextFeeder(i).Text = "0"
        Next
        ComboBox_OldProd.Items.Clear()
        TextBox_status1.BackColor = Color.WhiteSmoke
        TextBox_status1.Text = "等待产品流入..."
        TextBox_status2.BackColor = Color.WhiteSmoke
        TextBox_status2.Text = ""
        ComboBox_FLG1.SelectedIndex = 0
        'Button_ExpoureOK1.Enabled = False
        'Button_ExpoureOK2.Enabled = False
    End Sub

    ''' <summary>
    ''' 一键设置扭力角度供料器
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_AllSet_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_AllSet.Click
        Try
            If TextBox_head.Text > 30 Or TextBox_head.Text < 1 Then
                MsgBox("设定值超出范围！")
                TextBox_head.Text = "1"
                GoTo d
            End If
            If TextBox_end.Text > 30 Or TextBox_end.Text < 1 Then
                MsgBox("设定值超出范围！")
                TextBox_end.Text = "30"
                GoTo d
            End If
            If TextBox_end.Text < TextBox_head.Text Then
                MsgBox("最大值小于最小值")
                TextBox_head.Text = "1"
                TextBox_end.Text = "30"
                GoTo d
            End If


            Dim i As Integer = 0
            For i = Val(TextBox_head.Text) To (TextBox_end.Text)
                TextTorq(i).Text = TextBox_TorqAll.Text
                TextAngle(i).Text = TextBox_AngleAll.Text
                TextFeeder(i).Text = TextBox_FeederAll.Text
            Next
d:
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub


    ''' <summary>
    ''' 自动匹配按钮事件
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_Step2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Step2.Click
        ' Dim result As Integer
        Try
            Vision_NG = ""
            If Button_Step2.BackColor = Color.Lime Then
                StepOK2 = False
                Button_Step2.BackColor = Color.WhiteSmoke
                Button_Step2.UseVisualStyleBackColor = True
                'StepNO = "STEP0"
                'Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")

                TextBox_status2.Text = ""
                TextBox_status2.BackColor = Color.WhiteSmoke
            Else
                '加载产品
                FrmMain.TCmbPro.Text = TextBox_NewProdName.Text
                FrmMain.InitSetting()
                FrmMain.initPro(FrmMain.TCmbPro.Text)
                Chanpin = TextBox_NewProdName.Text
                '指定机器人速度20（）
                ComboBox_speed.SelectedIndex = 8
                '跑自动匹配程序
                Button_Step2.BackColor = Color.Lime

                'Dim Re_date As String
                StepNO = "STEP4"
                Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
                'Re_date = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")
                ' Re_date = SendDataAndRecvice(StepNO, "192.168.1.120", "2016")
                'MsgBox(Re_date)
            End If
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_TeachCAD_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_TeachCAD.Click

        If Button_TeachCAD.BackColor = Color.Lime Then
            Button_TeachCAD.BackColor = Color.WhiteSmoke
            Button_TeachCAD.UseVisualStyleBackColor = True
            Panel4.Visible = False
        Else
            Button_TeachCAD.BackColor = Color.Lime
            Panel4.Visible = True

            Button_TeachRB.BackColor = Color.WhiteSmoke
            Button_TeachRB.UseVisualStyleBackColor = True

        End If
    End Sub

    Private Sub Button_TeachRB_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_TeachRB.Click
        Button_TeachRB.BackColor = Color.Lime
        Panel4.Visible = True
        Button_TeachCAD.BackColor = Color.WhiteSmoke
        Button_TeachCAD.UseVisualStyleBackColor = True
        pointNO = 2
        TextBox_pointName.Text = "set01"
        TextBox_CADX.Text = CADPoint(pointNO).X
        TextBox_CADY.Text = CADPoint(pointNO).Y
        TextBox_CADZ.Text = CADPoint(pointNO).Z
        TextBox_CADC.Text = CADPoint(pointNO).C
        Try
            '指定机器人工具2(0,1,2,3,4,5)
            ComboBox_Tool.SelectedIndex = 2

            ''写入两个拍照点坐标
            'Select Case MsgBox("将当前拍照坐标写入数据库，是否修改？", MsgBoxStyle.YesNo)
            '    Case MsgBoxResult.Yes
            '        '确定要执行的
            '        Dim sql1 As String = ""
            '        Dim DBpath As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\cad_point.accdb"
            '        Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & DBpath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
            '        sql1 = " update " & " domino " & "set x=" & AutoAddPhotoPoint(1).X & ",y=" & AutoAddPhotoPoint(1).Y & ",z=" & AutoAddPhotoPoint(1).Z & ",c=" & AutoAddPhotoPoint(1).C & " where PointName=" & "'photo1cad'"
            '        HandleDB(sql1, cnStr)
            '        sql1 = " update " & " domino " & "set x=" & AutoAddPhotoPoint(2).X & ",y=" & AutoAddPhotoPoint(2).Y & ",z=" & AutoAddPhotoPoint(2).Z & ",c=" & AutoAddPhotoPoint(2).C & " where PointName=" & "'photo2cad'"
            '        HandleDB(sql1, cnStr)
            '    Case MsgBoxResult.No
            '        '取消要执行的

            'End Select
            CADPointName(0) = "photo1cad"
            CADPoint(0) = AutoAddPhotoPoint(1)
            CADPointName(1) = "photo2cad"
            CADPoint(1) = AutoAddPhotoPoint(2)
            CADPointName(2) = "set01"
            ''启动机器人示教
            'Dim robotpath As String = DebugPath & "\RobotConnect"
            ''写地址
            'File.WriteAllText(robotpath & "\pointPath.txt", DebugPath & "\Tool\" & TextBox_NewProdName.Text)
            ''开启机器人软件
            'Dim a As Integer
            'a = Shell(robotpath & "\RT.exe", vbNormalFocus)
            'If a = 0 Then
            '    MsgBox("机器人程序打开失败！！")
            '    GoTo d
            'End If
            ' flag(3) = True
            'Button_NextStep.Enabled = True
d:
        Catch ex As Exception
            MsgBox("OpenUploadProcess:" & ex.Message.ToString & ex.StackTrace.ToString)
        End Try
        '  End If


    End Sub

    Private Sub Button_NextPoint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_NextPoint.Click
        Button_PrePoint.Enabled = True
        If pointNO < 31 Then
            pointNO = pointNO + 1
            Select Case pointNO
                Case 0
                    CADPointName(pointNO) = "photo1cad"
                    Exit Select
                Case 1
                    CADPointName(pointNO) = "photo2cad"
                    Exit Select
                Case Is <= 10
                    CADPointName(pointNO) = "set0" & (pointNO - 1)
                    Exit Select
                Case Is > 10
                    CADPointName(pointNO) = "set" & (pointNO - 1)
                    If pointNO = 31 Then
                        Button_NextPoint.Enabled = False
                    End If
                    Exit Select
            End Select

        End If
        TextBox_pointName.Text = CADPointName(pointNO)
        TextBox_CADX.Text = CADPoint(pointNO).X
        TextBox_CADY.Text = CADPoint(pointNO).Y
        TextBox_CADZ.Text = CADPoint(pointNO).Z
        TextBox_CADC.Text = CADPoint(pointNO).C
    End Sub

    Private Sub Button_PrePoint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_PrePoint.Click
        If pointNO > 0 Then
            pointNO = pointNO - 1
            Button_NextPoint.Enabled = True
            Select Case pointNO
                Case 0
                    CADPointName(pointNO) = "photo1cad"
                    Button_PrePoint.Enabled = False
                    Exit Select
                Case 1
                    CADPointName(pointNO) = "photo2cad"
                    Exit Select
                Case Is <= 10
                    CADPointName(pointNO) = "set0" & (pointNO - 1)
                    Exit Select
                Case Is > 10
                    CADPointName(pointNO) = "set" & (pointNO - 1)

                    Exit Select
            End Select
        Else
            CADPointName(pointNO) = "photo1cad"
        End If
        TextBox_pointName.Text = CADPointName(pointNO)
        TextBox_CADX.Text = CADPoint(pointNO).X
        TextBox_CADY.Text = CADPoint(pointNO).Y
        TextBox_CADZ.Text = CADPoint(pointNO).Z
        TextBox_CADC.Text = CADPoint(pointNO).C
    End Sub

    Private Sub TextBox_CADX_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox_CADX.TextChanged
        CADPoint(pointNO).X = Val(TextBox_CADX.Text)
    End Sub

    Private Sub TextBox_CADY_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox_CADY.TextChanged
        CADPoint(pointNO).Y = Val(TextBox_CADY.Text)
    End Sub

    Private Sub TextBox_CADZ_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox_CADZ.TextChanged
        CADPoint(pointNO).Z = Val(TextBox_CADZ.Text)
    End Sub

    Private Sub TextBox_CADC_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox_CADC.TextChanged
        CADPoint(pointNO).C = Val(TextBox_CADC.Text)
    End Sub

    Private Sub ComboBox_FLG1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox_FLG1.SelectedIndexChanged
        If ComboBox_FLG1.SelectedItem.ToString = "R" Then
            CADPoint(pointNO).FLG1 = 4 '右手姿态
        Else
            CADPoint(pointNO).FLG1 = 0 '左手姿态
        End If

    End Sub

    Private Sub Button_PointWrite_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_PointWrite.Click
        Try
            If TextBox_NewProdName.Text = "" Then
                MsgBox("新建的产品名未指定！")
                GoTo d
            End If
            Dim SavePath As String = ToolPath & "\" & TextBox_NewProdName.Text & "\cad_point.accdb"
            If File.Exists(SavePath) = True Then
                For i = 0 To 31
                    Dim cnStr As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & SavePath & ";Persist Security Info=False;Jet OLEDB:Database Password='1234';"
                    'Dim sql1 As String = " update " & " domino " & "set x=" & CADPoint(i).X & ",y=" & CADPoint(i).Y & ",z=" & CADPoint(i).Z & ",c=" & CADPoint(i).C & ",FLG1=" & CADPoint(i).FLG1 & " where PointName=" & "'" & CADPointName(i) & "'"
                    Dim sql1 As String = " update " & " cad_point " & "set x=" & CADPoint(i).X & ",y=" & CADPoint(i).Y & ",z=" & CADPoint(i).Z & ",c=" & CADPoint(i).C & " where PointName=" & "'" & CADPointName(i) & "' and Product='" & TextBox_NewProdName.Text & "'"
                    HandleMysql(sql1)
                Next
                flag(3) = True
                Button_NextStep.Enabled = True
                MsgBox("点位写入完成！")
            Else
                MsgBox("路径不存在！请检查路径：" & SavePath)
            End If
d:
        Catch ex As Exception
            MsgBox("点位写入发生错误：" & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_Exist_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Exist.Click
        Me.Close()
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If StepOK1 = True Then
            TextBox_status1.Text = "产品已经到位"
            TextBox_status1.BackColor = Color.Lime
            TextBox_status2.Text = "准备匹配模板"
        End If
        If StepOK2 = True Then
            TextBox_status2.Text = "自动匹配模板中,请等待..."
            TextBox_status2.BackColor = Color.Yellow
        End If
        TextBox_Err.Text = FrmMain.TextBox_RBErr.Text
        If StepOK3 = True Then
            TextBox_status2.Text = "匹配模板完成!请示教点位。"
            TextBox_status2.BackColor = Color.Lime
        End If
        'If Vision_NG = "FIRST" Then
        '    TextBox_status2.Text = "匹配模板失败!第一个拍照点无法识别！"
        '    TextBox_status2.BackColor = Color.Red
        'ElseIf Vision_NG = "SECOND" Then
        '    TextBox_status2.Text = "匹配模板失败!第二个拍照点无法识别！"
        '    TextBox_status2.BackColor = Color.Red
        'End If

    End Sub

    Private Sub Button_Step1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_Step1.Click
        Try
            If Button_Step1.BackColor = Color.Lime Then
                Button_Step1.BackColor = Color.WhiteSmoke
                Button_Step1.UseVisualStyleBackColor = True

            
                TextBox_status1.Text = ""
                Button_Step1.Text = "准备投入"

            Else
                Button_Step1.Text = "重新投入"
                Motor_dly = Val(TextBox_MotorDly.Text)
                Button_Step1.BackColor = Color.Lime
               

                Dim Re_date As String
                StepNO = "STEP0"
                'Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
                Re_date = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")

                TextBox_status1.Text = "等待产品流入 ..."
                '指定机器人速度20（）
                ComboBox_speed.SelectedIndex = 8
            End If
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
        

    End Sub
#Region "机器人移动按钮"
    'X+
    'Private Sub Button_AddX_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddX.MouseDown
    '    Globalfunc.SendDataNORePly("X,5", "192.168.1.120", "2016", "UTF8")
    '    ButtonUP(1, 0)
    'End Sub

    'Private Sub Button_AddX_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddX.MouseUp

    '    ButtonUP(1, 0)
    'End Sub
    ''X-
    'Private Sub Button_DecX_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecX.MouseDown
    '    JOG_MOV(lRobotID, "-1", "0", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(1, 1)
    'End Sub

    'Private Sub Button_DecX_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecX.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(1, 1)
    'End Sub
    ''Y+
    'Private Sub Button_AddY_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddY.MouseDown
    '    JOG_MOV(lRobotID, "1", "1", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(2, 0)
    'End Sub

    'Private Sub Button_AddY_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddY.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(2, 0)
    'End Sub
    ''Y-
    'Private Sub Button_DecY_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecY.MouseDown
    '    JOG_MOV(lRobotID, "-1", "1", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(2, 1)
    'End Sub

    'Private Sub Button_DecY_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecY.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(2, 1)
    'End Sub
    ''Z+
    'Private Sub Button_AddZ_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddZ.MouseDown
    '    JOG_MOV(lRobotID, "1", "2", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(3, 0)
    'End Sub

    'Private Sub Button_AddZ_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddZ.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(3, 0)
    'End Sub
    ''Z-
    'Private Sub Button_DecZ_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecZ.MouseDown
    '    JOG_MOV(lRobotID, "-1", "2", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(3, 1)
    'End Sub

    'Private Sub Button_DecZ_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecZ.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(3, 1)
    'End Sub
    ''C+
    'Private Sub Button_AddC_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddC.MouseDown
    '    JOG_MOV(lRobotID, "1", "5", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(6, 0)
    'End Sub

    'Private Sub Button_AddC_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_AddC.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(6, 0)
    'End Sub
    ''C-

    'Private Sub Button_DecC_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecC.MouseDown
    '    JOG_MOV(lRobotID, "-1", "5", ComboBox_stepType.SelectedIndex)
    '    ButtonDown(6, 1)
    'End Sub

    'Private Sub Button_DecC_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Button_DecC.MouseUp
    '    Request_Cancel("405")
    '    StopProgram(lRobotID, "0")
    '    ButtonUP(6, 1)
    'End Sub

    Private Sub ButtonDown(ByRef i As Integer, ByRef j As Integer)
        Dim JogMove As Boolean = False
        Select Case i
            Case 1  'x-ji
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddX.BackgroundImage = My.Resources.Resources.XADD2
                    Else
                        Button_AddX.BackgroundImage = My.Resources.Resources.J1ADD2
                    End If
                Else    '-
                    If JogMove = False Then
                        Button_DecX.BackgroundImage = My.Resources.Resources.XSUB2
                    Else
                        Button_DecX.BackgroundImage = My.Resources.Resources.J1SUB2
                    End If

                End If
            Case 2  'y-j2
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddY.BackgroundImage = My.Resources.Resources.YADD2
                    Else
                        Button_AddY.BackgroundImage = My.Resources.Resources.J2ADD2
                    End If

                Else    '-
                    If JogMove = False Then
                        Button_DecY.BackgroundImage = My.Resources.Resources.YSUB2
                    Else
                        Button_DecY.BackgroundImage = My.Resources.Resources.J2SUB2
                    End If

                End If
            Case 3  'z-j3
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddZ.BackgroundImage = My.Resources.Resources.ZADD2
                    Else
                        Button_AddZ.BackgroundImage = My.Resources.Resources.J3ADD2
                    End If

                Else    '-
                    If JogMove = False Then
                        Button_DecZ.BackgroundImage = My.Resources.Resources.ZSUB2
                    Else
                        Button_DecZ.BackgroundImage = My.Resources.Resources.J3SUB2
                    End If

                End If
            Case 4  'a-j4
                'If j = 0 Then   '+
                '    If JogMove = False Then
                '        Button_AddA.BackgroundImage = My.Resources.Resources.AADD2
                '    Else
                '        Button_AddA.BackgroundImage =My.Resources.Resources.J4ADD2
                '    End If

                'Else    '-
                '    If JogMove = False Then
                '        Button_DecA.BackgroundImage = My.Resources.Resources.ASUB2
                '    Else
                '        Button_DecA.BackgroundImage = My.Resources.Resources.J4SUB2
                '    End If

                'End If
            Case 5  'b-j5
                'If j = 0 Then   '+
                '    If JogMove = False Then
                '        Button_AddB.BackgroundImage = My.Resources.Resources.BADD2
                '    Else
                '        Button_AddB.BackgroundImage =My.Resources.Resources.J5ADD2
                '    End If

                'Else    '-
                '    If JogMove = False Then
                '        Button_DecB.BackgroundImage = My.Resources.Resources.BSUB2
                '    Else
                '        Button_DecB.BackgroundImage =My.Resources.Resources.J5SUB2
                '    End If

                'End If
            Case 6  'c-j6
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddC.BackgroundImage = My.Resources.Resources.CADD2
                    Else
                        Button_AddC.BackgroundImage = My.Resources.Resources.J6ADD2
                    End If

                Else    '-
                    If JogMove = False Then
                        Button_DecC.BackgroundImage = My.Resources.Resources.CSUB2
                    Else
                        Button_DecC.BackgroundImage = My.Resources.Resources.J6SUB2
                    End If

                End If
        End Select
    End Sub

    Private Sub ButtonUP(ByRef i As Integer, ByRef j As Integer)
        Dim JogMove As Boolean = False
        Select Case i
            Case 1  'x-ji
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddX.BackgroundImage = My.Resources.Resources.XADD1
                    Else
                        Button_AddX.BackgroundImage = My.Resources.Resources.J1ADD1
                    End If
                Else    '-
                    If JogMove = False Then
                        Button_DecX.BackgroundImage = My.Resources.Resources.XSUB1
                    Else
                        Button_DecX.BackgroundImage = My.Resources.Resources.J1SUB1
                    End If

                End If
            Case 2  'y-j2
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddY.BackgroundImage = My.Resources.Resources.YADD1
                    Else
                        Button_AddY.BackgroundImage = My.Resources.Resources.J2ADD1
                    End If

                Else    '-
                    If JogMove = False Then
                        Button_DecY.BackgroundImage = My.Resources.Resources.YSUB1
                    Else
                        Button_DecY.BackgroundImage = My.Resources.Resources.J2SUB1
                    End If

                End If
            Case 3  'z-j3
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddZ.BackgroundImage = My.Resources.Resources.ZADD1
                    Else
                        Button_AddZ.BackgroundImage = My.Resources.Resources.J3ADD1
                    End If

                Else    '-
                    If JogMove = False Then
                        Button_DecZ.BackgroundImage = My.Resources.Resources.ZSUB1
                    Else
                        Button_DecZ.BackgroundImage = My.Resources.Resources.J3SUB1
                    End If

                End If
            Case 4  'a-j4
                'If j = 0 Then   '+
                '    If JogMove = False Then
                '        Button_AddA.BackgroundImage = My.Resources.Resources.AADD1
                '    Else
                '        Button_AddA.BackgroundImage = My.Resources.Resources.J4ADD1
                '    End If

                'Else    '-
                '    If JogMove = False Then
                '        Button_DecA.BackgroundImage = My.Resources.Resources.ASUB1
                '    Else
                '        Button_DecA.BackgroundImage = My.Resources.Resources.J4SUB1
                '    End If

                'End If
            Case 5  'b-j5
                'If j = 0 Then   '+
                '    If JogMove = False Then
                '        Button_AddB.BackgroundImage = My.Resources.Resources.BADD1
                '    Else
                '        Button_AddB.BackgroundImage = My.Resources.Resources.J5ADD1
                '    End If

                'Else    '-
                '    If JogMove = False Then
                '        Button_DecB.BackgroundImage = My.Resources.Resources.BSUB1
                '    Else
                '        Button_DecB.BackgroundImage = My.Resources.Resources.J5SUB1
                '    End If

                'End If
            Case 6  'c-j6
                If j = 0 Then   '+
                    If JogMove = False Then
                        Button_AddC.BackgroundImage = My.Resources.Resources.CADD1
                    Else
                        Button_AddC.BackgroundImage = My.Resources.Resources.J6ADD1
                    End If

                Else    '-
                    If JogMove = False Then
                        Button_DecC.BackgroundImage = My.Resources.Resources.CSUB1
                    Else
                        Button_DecC.BackgroundImage = My.Resources.Resources.J6SUB1
                    End If

                End If
        End Select
    End Sub

    Private Sub ComboBox_Tool_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox_Tool.SelectedIndexChanged
        Dim code As Integer
        Dim sSendData As String
        Dim lStatus As Integer
        Dim SelectTool As String
        Try
            If ComboBox_Tool.SelectedIndex = 0 Then
                SelectTool = "0,0,0,0,0,0"
            Else
                SelectTool = TOOL(ComboBox_Tool.SelectedIndex)
            End If

            '302 参数值写入
            code = 302
            sSendData = "1" & Chr(10) & "1" & Chr(10) & "MEXTL" & Chr(10) & SelectTool
         

            '获取当前tool参数
            Dim commandCode As Integer
            'Dim sSendData As String
            ' Dim lStatus As Integer

            commandCode = 301  '获取参数
            sSendData = "1" & Chr(10) & "17" & Chr(10) & "MEXTL" & Chr(10)
            For i = 1 To 15
                sSendData += "MEXTL" & i & Chr(10)
            Next
            sSendData += "MEXTL16"

           

D:
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button_SVON_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_SVON.Click
        Try
            Motor_flag = True
            Button_SVON.BackColor = Color.YellowGreen
            Button_SVOFF.BackColor = Color.WhiteSmoke

            StepNO = "STONN"
            ' Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
            Dim Revedates As String           
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")
            '  ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
            MsgBox("机器人伺服打开")
        Catch ex As Exception
            MsgBox("机器人伺服打开错误：" & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_SVOFF_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_SVOFF.Click
        Motor_flag = False
        Button_SVON.BackColor = Color.WhiteSmoke
        Button_SVOFF.BackColor = Color.YellowGreen
        Try
            StepNO = "STOFF"
            Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
            MsgBox("机器人伺服关闭")
        Catch ex As Exception
            MsgBox("机器人伺服关闭错误：" & ex.Message.ToString)
        End Try

    End Sub

    Private Sub ComboBox_speed_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox_speed.SelectedIndexChanged
        Try
            Dim sSendData As String
            Dim lStatus As Integer
            Dim CommandCode As Integer = 404
            '
            sSendData = ComboBox_speed.Text
        

        Catch ex As Exception
            MsgBox("错误：" & ex.Message.ToString)
        End Try
    End Sub
#End Region
    '#################################################
    '检测工具1 曝光修改
    'Private Sub Button_ExpoureOK1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        tmpfifo1.Operator.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure1.Text)
    '        myAcqFifo.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure1.Text)

    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    ''' <summary>
    ''' 加载相机工具
    ''' </summary>
    ''' <remarks></remarks>
    'Private Sub LoadCameraTool()
    '    Dim newProName As String = TextBox_NewProdName.Text
    '    Try
    '        If newProName <> "" Then
    '            Dim path As String = DebugPath & "\Tool\" & newProName & "\"
    '            For i = 0 To Cam_num - 1
    '                gTBCheck(i) = Nothing
    '                gTBCheck(i) = New CogToolBlock
    '            Next

    '            If Dir(path, FileAttribute.Directory) <> "" Then '如果路径存在
    '                For i = 0 To Cam_num - 1
    '                    gTBCheck(i) = CogSerializer.LoadObjectFromFile(path & "TBCheck" & (i + 1).ToString & ".vpp")
    '                    gTBCheck(i).LastRunRecordEnable = Cognex.VisionPro.Implementation.Internal.CogUserToolLastRunRecordConstants.CompositeSubToolRecords
    '                Next
    '                '加载相机工具1
    '                tmpfifo1 = gTBCheck(0).Tools.Item("相机设定")
    '                tmpCalibCB1 = gTBCheck(0).Tools.Item("CogCalibCheckerboardTool1")
    '                tmpPMAlign1 = gTBCheck(0).Tools.Item("CogPMAlignTool1")
    '                'TextBox_Exposure1.Text = tmpfifo1.Operator.OwnedExposureParams.Exposure
    '                tmpPMAlign1.SearchRegion = Nothing
    '                tmpPMAlign1.Pattern.TrainImageMask = Nothing
    '                '加载相机工具2
    '                tmpfifo2 = gTBCheck(1).Tools.Item("相机设定")
    '                tmpCalibCB2 = gTBCheck(1).Tools.Item("CogCalibCheckerboardTool1")
    '                tmpPMAlign2 = gTBCheck(1).Tools.Item("CogPMAlignTool1")
    '                TextBox_Exposure2.Text = tmpfifo2.Operator.OwnedExposureParams.Exposure
    '                tmpPMAlign2.SearchRegion = Nothing
    '                tmpPMAlign2.Pattern.TrainImageMask = Nothing
    '                '加载相机工具3
    '                tmpfifo3 = gTBCheck(2).Tools.Item("相机设定")
    '                tmpCalibCB3 = gTBCheck(2).Tools.Item("CogCalibCheckerboardTool1")
    '                tmpPMAlign3 = gTBCheck(2).Tools.Item("CogPMAlignTool1")
    '                TextBox_Exposure3.Text = tmpfifo3.Operator.OwnedExposureParams.Exposure
    '                tmpPMAlign3.SearchRegion = Nothing
    '                tmpPMAlign3.Pattern.TrainImageMask = Nothing
    '                '计算比率

    '                Dim Transform As Cognex.VisionPro.ICogTransform2D
    '                Dim x1 As Double
    '                Dim y1 As Double
    '                Dim x2 As Double
    '                Dim y2 As Double
    '                Dim x3 As Double
    '                Dim y3 As Double

    '                '计算TOOL1 比率
    '                Transform = tmpCalibCB1.Calibration.GetComputedUncalibratedFromCalibratedTransform
    '                Transform.MapPoint(0, 0, HomeX(0), HomeY(0)) '获取原点

    '                Transform.MapPoint(20, 20, x1, y1)
    '                Transform.MapPoint(-20, -20, x2, y2)

    '                Transform.MapPoint(20, 0, x3, y3)
    '                TrainRate(0) = (Abs(x1 - x2) + Abs(y1 - y2)) / 80

    '                trainAngle(0) = Atan2(y3 - HomeY(0), x3 - HomeX(0)) / Acos(-1) * 180
    '                '计算TOOL2 比率
    '                Transform = tmpCalibCB2.Calibration.GetComputedUncalibratedFromCalibratedTransform
    '                Transform.MapPoint(0, 0, HomeX(1), HomeY(1)) '获取原点

    '                Transform.MapPoint(20, 20, x1, y1)
    '                Transform.MapPoint(-20, -20, x2, y2)

    '                Transform.MapPoint(20, 0, x3, y3)
    '                TrainRate(1) = (Abs(x1 - x2) + Abs(y1 - y2)) / 80
    '                trainAngle(1) = Atan2(y3 - HomeY(1), x3 - HomeX(1)) / Acos(-1) * 180


    '            Else
    '                MsgBox("加载产品路径不存在") '错误信息写入log
    '            End If

    '        End If

    '    Catch ex As Exception
    '        MsgBox(ex.Message)
    '    End Try
    'End Sub

    'Private Sub Button_TakePhoto1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    CircularPhotography(1)
    'End Sub

    'Private Sub Button_takePhoto2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    tmpfifo2.Run()
    'End Sub


    'Private Sub Button_ExpoureOK2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    tmpfifo1.Operator.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure1.Text)
    'End Sub

    ''' <summary>
    ''' 初始化相机
    ''' </summary>
    ''' <param name="NO"></param>
    ''' <remarks></remarks>
    'Private Sub initFifo(ByVal NO As String)

    '    Try
    '        Const VIDEO_FORMAT As String = "Generic GigEVision (Mono)"
    '        myFrameGrabbers = New CogFrameGrabbers()
    '        myFrameGrabber = myFrameGrabbers(0)
    '        myAcqFifo = myFrameGrabber.CreateAcqFifo(VIDEO_FORMAT,
    '                                                  Cognex.VisionPro.CogAcqFifoPixelFormatConstants.Format8Grey, 0, False)
    '        Select Case NO
    '            Case 1
    '                'myAcqFifo.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure1.Text)
    '            Case 2
    '                myAcqFifo.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure2.Text)
    '            Case 3
    '                myAcqFifo.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure3.Text)
    '        End Select

    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    ''' <summary>
    ''' 训练模板1
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    'Private Sub Button_HighTrain1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        TBcheck_NO = 1
    '        CogRecordDisplay1.StopLiveDisplay()
    '        Button_TakePhoto1.Text = "连续取像"
    '        Button_TakePhoto1.BackColor = Color.WhiteSmoke
    '        Button_TakePhoto1.UseVisualStyleBackColor = True
    '        tmpfifo1.Run()
    '        tmpCalibCB1.Run()
    '        tmpPMAlign1.InputImage = tmpCalibCB1.OutputImage
    '        tmpPMAlign1.Pattern.TrainImage = tmpCalibCB1.OutputImage
    '        FrmPMAlignTool.CogPMAlignEditV21.Subject = tmpPMAlign1
    '        FrmPMAlignTool.ShowDialog()
    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    ''' <summary>
    ''' 保存模板1及P_31
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_SaveTool1_Click_1(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim path As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\"
            If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
                MsgBox("路径不存在:" & path)
            Else
                CogSerializer.SaveObjectToFile(gTBCheck(0), path & "TBCheck1.vpp")
                MsgBox("模板1保存成功！")
                TeachPhotoPoint(1)
                Button_NextStep.Enabled = True
            End If

        Catch ex As Exception
            MsgBox("保存失败！错误：" & ex.Message.ToString)
        End Try

    End Sub

    'Private Sub Button_TakePhoto2_Click_1(ByVal sender As Object, ByVal e As EventArgs) Handles Button_TakePhoto2.Click
    '    CircularPhotography(2)
    'End Sub

    ''' <summary>
    ''' 工具2曝光调整
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    'Private Sub Button_ExpoureOK2_Click_1(ByVal sender As Object, ByVal e As EventArgs)
    '    Try

    '        tmpfifo2.Operator.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure2.Text)
    '        myAcqFifo.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure2.Text)

    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    ''' <summary>
    ''' 训练模板2
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    'Private Sub Button_train2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        TBcheck_NO = 2
    '        CogRecordDisplay2.StopLiveDisplay()
    '        Button_TakePhoto2.Text = "连续取像"
    '        Button_TakePhoto2.BackColor = Color.WhiteSmoke
    '        Button_TakePhoto2.UseVisualStyleBackColor = True
    '        tmpfifo2.Run()
    '        tmpCalibCB2.Run()
    '        tmpPMAlign2.InputImage = tmpCalibCB2.OutputImage
    '        tmpPMAlign2.Pattern.TrainImage = tmpCalibCB2.OutputImage
    '        FrmPMAlignTool.CogPMAlignEditV21.Subject = tmpPMAlign2

    '        FrmPMAlignTool.ShowDialog()
    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub


    ''' <summary>
    ''' 保存模板2及P_32
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_SaveTool2_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim path As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\"
            If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
                MsgBox("路径不存在:" & path)
            Else
                CogSerializer.SaveObjectToFile(gTBCheck(1), path & "TBCheck2.vpp")
                MsgBox("模板2保存成功！")
                TeachPhotoPoint(2)
                Button_NextStep.Enabled = True
            End If
        Catch ex As Exception
            MsgBox("保存失败！错误：" & ex.Message.ToString)
        End Try
    End Sub

    ''' <summary>
    ''' 获取机器人坐标至点位区
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_ReadPoint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_ReadPoint.Click
        TextBox_CADX.Text = TextBox_PX.Text
        TextBox_CADY.Text = TextBox_PY.Text
        TextBox_CADZ.Text = TextBox_PZ.Text
        TextBox_CADC.Text = TextBox_PC.Text
    End Sub

    ''' <summary>
    ''' 循环拍照
    ''' </summary>
    ''' <param name="NO">工具编号</param>
    ''' <remarks></remarks>
    'Private Sub CircularPhotography(ByVal NO As String)
    '    Try
    '        If NO = 1 Then
    '            If Button_TakePhoto1.Text = "连续取像" Then
    '                Button_ExpoureOK1.Enabled = True
    '                '关闭取像3
    '                CogRecordDisplay3.StopLiveDisplay()
    '                Button_TakePhoto3.Text = "连续取像"
    '                Button_TakePhoto3.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto3.UseVisualStyleBackColor = True
    '                '关闭取像2
    '                CogRecordDisplay2.StopLiveDisplay()
    '                Button_TakePhoto2.Text = "连续取像"
    '                Button_TakePhoto2.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto2.UseVisualStyleBackColor = True
    '                CogRecordDisplay1.Fit()
    '                '开启取像1
    '                initFifo(1)
    '                CogRecordDisplay1.StartLiveDisplay(myAcqFifo, False)
    '                CogRecordDisplay1.Fit()

    '                Dim LIN1 As New CogLine
    '                LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '                CogRecordDisplay1.StaticGraphics.Add(LIN1, "")

    '                Dim LIN2 As New CogLine
    '                LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
    '                CogRecordDisplay1.StaticGraphics.Add(LIN2, "")

    '                Button_TakePhoto1.Text = "停止取像"
    '                Button_TakePhoto1.BackColor = Color.Lime
    '                GroupBox2.Visible = False
    '            Else
    '                Button_ExpoureOK1.Enabled = False
    '                GroupBox2.Visible = True
    '                Button_TakePhoto1.Text = "连续取像"
    '                CogRecordDisplay1.StopLiveDisplay()
    '                CogRecordDisplay1.Fit()
    '                Button_TakePhoto1.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto1.UseVisualStyleBackColor = True

    '            End If
    '            RateZoom = 1
    '        ElseIf NO = 2 Then
    '            If Button_TakePhoto2.Text = "连续取像" Then
    '                Button_ExpoureOK2.Enabled = True
    '                '关闭取像1
    '                CogRecordDisplay1.StopLiveDisplay()
    '                Button_TakePhoto1.Text = "连续取像"
    '                Button_TakePhoto1.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto1.UseVisualStyleBackColor = True
    '                '关闭取像3
    '                CogRecordDisplay3.StopLiveDisplay()
    '                Button_TakePhoto3.Text = "连续取像"
    '                Button_TakePhoto3.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto3.UseVisualStyleBackColor = True

    '                '开启取像2
    '                initFifo(2)

    '                CogRecordDisplay2.StartLiveDisplay(myAcqFifo, False)
    '                CogRecordDisplay2.Fit()
    '                Dim LIN1 As New CogLine
    '                LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '                CogRecordDisplay2.StaticGraphics.Add(LIN1, "")

    '                Dim LIN2 As New CogLine
    '                LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
    '                CogRecordDisplay2.StaticGraphics.Add(LIN2, "")
    '                Button_TakePhoto2.Text = "停止取像"
    '                Button_TakePhoto2.BackColor = Color.Lime
    '                GroupBox5.Visible = False
    '            Else
    '                Button_ExpoureOK2.Enabled = False
    '                '关闭取像2
    '                GroupBox5.Visible = True
    '                CogRecordDisplay2.StopLiveDisplay()
    '                CogRecordDisplay2.Fit()
    '                Button_TakePhoto2.Text = "连续取像"
    '                Button_TakePhoto2.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto2.UseVisualStyleBackColor = True
    '            End If
    '            RateZoom = 1
    '        ElseIf NO = 3 Then
    '            If Button_TakePhoto3.Text = "连续取像" Then
    '                Button_ExpoureOK2.Enabled = True
    '                '关闭取像1
    '                CogRecordDisplay1.StopLiveDisplay()
    '                Button_TakePhoto1.Text = "连续取像"
    '                Button_TakePhoto1.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto1.UseVisualStyleBackColor = True
    '                '关闭取像2
    '                CogRecordDisplay2.StopLiveDisplay()
    '                Button_TakePhoto2.Text = "连续取像"
    '                Button_TakePhoto2.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto2.UseVisualStyleBackColor = True

    '                '开启取像2
    '                initFifo(3)

    '                CogRecordDisplay3.StartLiveDisplay(myAcqFifo, False)
    '                CogRecordDisplay3.Fit()
    '                Dim LIN1 As New CogLine
    '                LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '                CogRecordDisplay3.StaticGraphics.Add(LIN1, "")

    '                Dim LIN2 As New CogLine
    '                LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
    '                CogRecordDisplay3.StaticGraphics.Add(LIN2, "")
    '                Button_TakePhoto3.Text = "停止取像"
    '                Button_TakePhoto3.BackColor = Color.Lime
    '                GroupBox8.Visible = False
    '            Else
    '                Button_ExpoureOK3.Enabled = False
    '                '关闭取像2
    '                GroupBox8.Visible = True
    '                CogRecordDisplay3.StopLiveDisplay()
    '                CogRecordDisplay3.Fit()
    '                Button_TakePhoto3.Text = "连续取像"
    '                Button_TakePhoto3.BackColor = Color.WhiteSmoke
    '                Button_TakePhoto3.UseVisualStyleBackColor = True
    '            End If
    '            RateZoom = 1
    '        End If


    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    ''' <summary>
    ''' 示教拍照点至机器人
    ''' </summary>
    ''' <param name="pNO">1:P_31;2:P_32</param>
    ''' <remarks></remarks>
    Private Sub TeachPhotoPoint(ByVal pNO As String)
        ''写入拍照点坐标
        Select Case MsgBox("当前机器人点位保存至拍照点" & pNO & "，是否修改？", MsgBoxStyle.YesNo)
            Case MsgBoxResult.Yes
                If pNO = 1 Or pNO = 2 Or pNO = 3 Or pNO = 4 Then
                    Try
                        Globalfunc.SendDataNORePly(pNO, "192.168.1.120", "2014", "UTF8")

                    Catch ex As Exception
                        MsgBox("SendMessage:" & ex.Message.ToString)
                    End Try
                Else
                    MsgBox("点位编号错误，只能指定1,2,3,4！pNO=" & pNO)
                End If

            Case MsgBoxResult.No
                '取消要执行的

        End Select
    End Sub

    Private Sub Button_RESET_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_RESET.Click
        '复位报警407
        Dim sSendData As String = ""
        Dim lStatus As Integer
        Dim Code As Integer
        Try
            Code = 407
         
        Catch ex As Exception

        End Try
    End Sub



    Private Sub Button_IO_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button_IO.Click
        'Try
        '    If My.Application.OpenForms.Item("FormIO") IsNot Nothing Then
        '        FormIO.Close()
        '    End If
        '    FormIO.StartPosition = FormStartPosition.CenterScreen
        '    FormIO.ShowDialog()
        'Catch ex As Exception
        '    MsgBox("错误:" & ex.Message.ToString)
        'End Try
    End Sub
#Region "模板1事件"

#End Region

    Dim pixelX As Double
    Dim pixelY As Double
    Dim pixelR As Double

    Dim regionCenterX As Double
    Dim regionCenterY As Double
    Dim regionCenterR As Double

    ''' <summary>
    ''' 相机设置
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button_FifoSet1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            FrmFifoTool.CogAcqFifoEditV21.Subject = tmpfifo1
            FrmFifoTool.ShowDialog()

        Catch ex As Exception
            MsgBox("错误：" & ex.Message.ToString)
        End Try
    End Sub

    ''' <summary>
    ''' 适合图像1
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    'Private Sub Button_Fit1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        CogRecordDisplay1.Fit()
    '        RateZoom = 1
    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_ZoomIn1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        RateZoom = RateZoom * 0.5
    '        CogRecordDisplay1.Zoom = 0.5 * CogRecordDisplay1.Zoom
    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try

    'End Sub

    'Private Sub Button_ZoomOut1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        RateZoom = RateZoom * 2
    '        CogRecordDisplay1.Zoom = 2 * CogRecordDisplay1.Zoom
    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_Move1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        CogRecordDisplay1.MouseMode = CogDisplayMouseModeConstants.Pan

    '        If Button_Move1.BackColor = Color.Lime Then
    '            Button_Move1.BackColor = Color.WhiteSmoke
    '            Button_Move1.UseVisualStyleBackColor = True
    '        Else
    '            Button_Move1.BackColor = Color.Lime
    '            Button_Pointer1.BackColor = Color.WhiteSmoke
    '            Button_Pointer1.UseVisualStyleBackColor = True
    '            GetRegion1 = False
    '            Button_MVcenter1.BackColor = Color.WhiteSmoke
    '            Button_MVcenter1.UseVisualStyleBackColor = True
    '            MoveRegion1 = False
    '        End If

    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_Pointer1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        If GetRegion1 = True Then
    '            GetRegion1 = False
    '            Button_Pointer1.BackColor = Color.WhiteSmoke
    '            Button_Pointer1.UseVisualStyleBackColor = True
    '        Else
    '            GetRegion1 = True
    '            Button_Pointer1.BackColor = Color.Lime
    '            Button_Move1.BackColor = Color.WhiteSmoke
    '            Button_Move1.UseVisualStyleBackColor = True
    '            Button_MVcenter1.BackColor = Color.WhiteSmoke
    '            Button_MVcenter1.UseVisualStyleBackColor = True
    '            MoveRegion1 = False
    '        End If

    '        tmpfifo1.Run() '相机运行
    '        tmpCalibCB1.Run() '标定工具运行
    '        tmpPMAlign1.InputImage = tmpCalibCB1.OutputImage
    '        tmpPMAlign1.Pattern.TrainImage = tmpCalibCB1.OutputImage

    '        CogRecordDisplay1.MouseMode = CogDisplayMouseModeConstants.Pointer

    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub
    ''' <summary>
    ''' 鼠标按下获取点位
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    'Private Sub CogRecordDisplay1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    '    Try


    '        RECTX1 = (CogRecordDisplay1.PointToClient(MousePosition).X / Rate0 - HomeX(0))
    '        RECTY1 = (CogRecordDisplay1.PointToClient(MousePosition).Y / Rate0 - HomeY(0))

    '    Catch ex As Exception

    '    End Try
    '    '        GoTo D
    '    '        If MoveRegion1 = False Then
    '    '            GoTo D
    '    '        End If
    '    '        Try

    '    '            If e.Button = Windows.Forms.MouseButtons.Left Then
    '    '                If ComboBox_Region1.SelectedIndex <> -1 Then '有选择
    '    '                    Select Case ComboBox_Region1.SelectedItem.ToString
    '    '                        Case "矩形"
    '    '                            If e.Button = Windows.Forms.MouseButtons.Left Then
    '    '                                '图像平移量
    '    '                                Dim pixPanX As Double = CogRecordDisplay1.PanX
    '    '                                Dim pixPanY As Double = CogRecordDisplay1.PanY
    '    '                                '转换至像素位置
    '    '                                Dim pixelX1 As String = HomeX(0) + (RECTX1 / RateZoom) - pixPanX '329.5 + (RECTX1 / RateZoom) - pixPanX
    '    '                                Dim pixelY1 As String = HomeY(0) + (RECTY1 / RateZoom) - pixPanY '247 + (RECTY1 / RateZoom) - pixPanY
    '    '                                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '    '                                '训练区域像素位置               
    '    '                                Dim TrainRaginPixel As New CogRectangleAffine
    '    '                                'TrainRagin.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '    '                                TrainRaginPixel.CenterX = pixelX1
    '    '                                TrainRaginPixel.CenterY = pixelY1
    '    '                                If RgSideX > 0 And RgSideY > 0 Then
    '    '                                    CogRecordDisplay1.StaticGraphics.Clear()
    '    '                                    TrainRaginPixel.SideXLength = RgSideX
    '    '                                    TrainRaginPixel.SideYLength = RgSideY
    '    '                                    CogRecordDisplay1.StaticGraphics.Add(TrainRaginPixel, "")
    '    '                                End If

    '    '                                '训练区域标定位置
    '    '                                Dim TrainRaginCalib As New CogRectangleAffine

    '    '                                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(0)) / TrainRate(0)
    '    '                                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(0)) / TrainRate(0)

    '    '                                If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
    '    '                                    TrainRaginCalib.SideYLength = TrainRaginPixel.SideXLength / TrainRate(0)
    '    '                                    TrainRaginCalib.SideXLength = TrainRaginPixel.SideYLength / TrainRate(0)
    '    '                                    tmpPMAlign1.Pattern.TrainRegion = TrainRaginCalib
    '    '                                    tmpPMAlign1.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '    '                                    tmpPMAlign1.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '    '                                End If
    '    '                            End If
    '    '                        Case "圆形"
    '    '                            If e.Button = Windows.Forms.MouseButtons.Left Then
    '    '                                '图像平移量
    '    '                                Dim pixPanX As Double = CogRecordDisplay1.PanX
    '    '                                Dim pixPanY As Double = CogRecordDisplay1.PanY
    '    '                                '转换至像素位置
    '    '                                Dim pixelX1 As String = HomeX(0) + (RECTX1 / RateZoom) - pixPanX
    '    '                                Dim pixelY1 As String = HomeY(0) + (RECTY1 / RateZoom) - pixPanY
    '    '                                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '    '                                Dim TrainRaginPixel As New CogCircle
    '    '                                '选取点即原点
    '    '                                TrainRaginPixel.CenterX = pixelX1
    '    '                                TrainRaginPixel.CenterY = pixelY1
    '    '                                If RgR > 0 Then
    '    '                                    CogRecordDisplay1.StaticGraphics.Clear()
    '    '                                    TrainRaginPixel.Radius = RgR
    '    '                                    ' TextBox_R1.Text = TrainRagin.Radius / 7.933
    '    '                                    CogRecordDisplay1.StaticGraphics.Add(TrainRaginPixel, "")
    '    '                                End If

    '    '                                Dim CenterPoint As New CogPointMarker
    '    '                                CenterPoint.SizeInScreenPixels = 10
    '    '                                CenterPoint.X = TrainRaginPixel.CenterX
    '    '                                CenterPoint.Y = TrainRaginPixel.CenterY
    '    '                                CogRecordDisplay1.StaticGraphics.Add(CenterPoint, "")

    '    '                                'Dim LIN1 As New CogLine
    '    '                                'LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '    '                                'CogRecordDisplay1.StaticGraphics.Add(LIN1, "")

    '    '                                'Dim LIN2 As New CogLine
    '    '                                'LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
    '    '                                'CogRecordDisplay1.StaticGraphics.Add(LIN2, "")
    '    '                                '训练区域标定位置
    '    '                                Dim TrainRaginCalib As New CogCircle
    '    '                                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(0)) / TrainRate(0)
    '    '                                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(0)) / TrainRate(0)

    '    '                                If TrainRaginPixel.Radius Then
    '    '                                    TrainRaginCalib.Radius = TrainRaginPixel.Radius / TrainRate(0)
    '    '                                    tmpPMAlign1.Pattern.TrainRegion = TrainRaginCalib
    '    '                                    tmpPMAlign1.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '    '                                    tmpPMAlign1.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '    '                                End If

    '    '                            End If
    '    '                    End Select
    '    '                End If
    '    '            End If
    '    '        Catch ex As Exception
    '    '            MsgBox("错误" & ex.Message.ToString)
    '    '        End Try
    '    'D:
    'End Sub

    ''' <summary>
    ''' 鼠标移动获取点位
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    '    Private Sub CogRecordDisplay1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    '        '获取收搜区域终点
    '        If GetRegion1 = False Then
    '            GoTo D
    '        End If
    '        Try
    '            If e.Button = Windows.Forms.MouseButtons.Left Then
    '                If ComboBox_Region1.SelectedIndex <> -1 Then '有选择
    '                    Dim str As String = ComboBox_Region1.SelectedItem.ToString
    '                    GetRegion(CogRecordDisplay1, tmpPMAlign1, str, RgSideX, RgSideY, 0)
    '                End If
    '                ' 
    '            End If
    '        Catch ex As Exception

    '        End Try
    'D:

    '        '移动搜索区域

    '        If MoveRegion1 = False Then
    '            GoTo E
    '        End If
    '        RECTX1 = (CogRecordDisplay1.PointToClient(MousePosition).X / Rate0 - HomeX(0))
    '        RECTY1 = (CogRecordDisplay1.PointToClient(MousePosition).Y / Rate0 - HomeY(0))

    '        If e.Button = Windows.Forms.MouseButtons.Left Then
    '            If ComboBox_Region1.SelectedIndex <> -1 Then '有选择
    '                Dim str As String = ComboBox_Region1.SelectedItem.ToString
    '                GetStartPoint(CogRecordDisplay1, tmpPMAlign1, str, RgSideX, RgSideY, 0)
    '            End If
    '        End If

    'E:


    '        '        If GetRegion1 = False Then
    '        '            GoTo D
    '        '        End If
    '        '        Try
    '        '            If e.Button = Windows.Forms.MouseButtons.Left Then
    '        '                If ComboBox_Region1.SelectedIndex <> -1 Then '有选择
    '        '                    '终点坐标
    '        '                    RECTX2 = (CogRecordDisplay1.PointToClient(MousePosition).X / Rate0 - HomeX(0))
    '        '                    RECTY2 = (CogRecordDisplay1.PointToClient(MousePosition).Y / Rate0 - HomeY(0))
    '        '                    Select Case ComboBox_Region1.SelectedItem.ToString
    '        '                        Case "矩形"
    '        '                            If e.Button = Windows.Forms.MouseButtons.Left Then
    '        '                                '图像平移量
    '        '                                Dim pixPanX As Double = CogRecordDisplay1.PanX
    '        '                                Dim pixPanY As Double = CogRecordDisplay1.PanY
    '        '                                '转换至像素位置
    '        '                                Dim pixelX1 As String = HomeX(0) + (RECTX1 / RateZoom) - pixPanX '329.5 + (RECTX1 / RateZoom) - pixPanX
    '        '                                Dim pixelY1 As String = HomeY(0) + (RECTY1 / RateZoom) - pixPanY '247 + (RECTY1 / RateZoom) - pixPanY
    '        '                                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '        '                                Dim pixelX2 As String = HomeX(0) + (RECTX2 / RateZoom) - pixPanX
    '        '                                Dim pixelY2 As String = HomeY(0) + (RECTY2 / RateZoom) - pixPanY
    '        '                                '训练区域像素位置               
    '        '                                Dim TrainRaginPixel As New CogRectangleAffine
    '        '                                'TrainRagin.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '        '                                TrainRaginPixel.CenterX = pixelX1 + (pixelX2 - pixelX1) / 2  'regionCenterX
    '        '                                TrainRaginPixel.CenterY = pixelY1 + (pixelY2 - pixelY1) / 2 'regionCenterY
    '        '                                If pixelX2 <> pixelX1 And pixelY1 <> pixelY2 Then
    '        '                                    CogRecordDisplay1.StaticGraphics.Clear()
    '        '                                    TrainRaginPixel.SideXLength = Abs(pixelX2 - pixelX1)
    '        '                                    TrainRaginPixel.SideYLength = Abs(pixelY2 - pixelY1)
    '        '                                    RgSideX = TrainRaginPixel.SideXLength
    '        '                                    RgSideY = TrainRaginPixel.SideYLength
    '        '                                    CogRecordDisplay1.StaticGraphics.Add(TrainRaginPixel, "")
    '        '                                End If

    '        '                                Dim CenterPoint As New CogPointMarker

    '        '                                CenterPoint.SizeInScreenPixels = 10
    '        '                                CenterPoint.X = TrainRaginPixel.CenterX
    '        '                                CenterPoint.Y = TrainRaginPixel.CenterY
    '        '                                CogRecordDisplay1.StaticGraphics.Add(CenterPoint, "")

    '        '                                '训练区域标定位置
    '        '                                Dim TrainRaginCalib As New CogRectangleAffine

    '        '                                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(0)) / TrainRate(0)
    '        '                                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(0)) / TrainRate(0)

    '        '                                If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
    '        '                                    TrainRaginCalib.SideYLength = TrainRaginPixel.SideXLength / TrainRate(0)
    '        '                                    TrainRaginCalib.SideXLength = TrainRaginPixel.SideYLength / TrainRate(0)
    '        '                                    tmpPMAlign1.Pattern.TrainRegion = TrainRaginCalib
    '        '                                    tmpPMAlign1.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '        '                                    tmpPMAlign1.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '        '                                End If
    '        '                            End If
    '        '                        Case "圆形"

    '        '                            If e.Button = Windows.Forms.MouseButtons.Left Then

    '        '                                '图像平移量
    '        '                                Dim pixPanX As Double = CogRecordDisplay1.PanX
    '        '                                Dim pixPanY As Double = CogRecordDisplay1.PanY
    '        '                                '转换至像素位置
    '        '                                Dim pixelX1 As String = HomeX(0) + (RECTX1 / RateZoom) - pixPanX
    '        '                                Dim pixelY1 As String = HomeY(0) + (RECTY1 / RateZoom) - pixPanY
    '        '                                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '        '                                '转换至像素位置
    '        '                                Dim pixelX2 As String = HomeX(0) + (RECTX2 / RateZoom) - pixPanX
    '        '                                Dim pixelY2 As String = HomeY(0) + (RECTY2 / RateZoom) - pixPanY

    '        '                                Dim TrainRaginPixel As New CogCircle
    '        '                                '选取点即原点
    '        '                                TrainRaginPixel.CenterX = pixelX1
    '        '                                TrainRaginPixel.CenterY = pixelY1
    '        '                                If pixelX2 <> pixelX1 And pixelY1 <> pixelY2 Then
    '        '                                    CogRecordDisplay1.StaticGraphics.Clear()
    '        '                                    If MoveRegion1 = True Then
    '        '                                        TrainRaginPixel.Radius = RgR
    '        '                                    Else
    '        '                                        TrainRaginPixel.Radius = Abs(pixelY2 - pixelY1)
    '        '                                        RgR = TrainRaginPixel.Radius
    '        '                                    End If
    '        '                                    ' TextBox_R1.Text = TrainRagin.Radius / 7.933
    '        '                                    CogRecordDisplay1.StaticGraphics.Add(TrainRaginPixel, "")
    '        '                                End If


    '        '                                Dim CenterPoint As New CogPointMarker

    '        '                                CenterPoint.SizeInScreenPixels = 10
    '        '                                CenterPoint.X = TrainRaginPixel.CenterX
    '        '                                CenterPoint.Y = TrainRaginPixel.CenterY
    '        '                                CogRecordDisplay1.StaticGraphics.Add(CenterPoint, "")

    '        '                                'Dim LIN1 As New CogLine
    '        '                                'LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '        '                                'CogRecordDisplay1.StaticGraphics.Add(LIN1, "")

    '        '                                'Dim LIN2 As New CogLine
    '        '                                'LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
    '        '                                'CogRecordDisplay1.StaticGraphics.Add(LIN2, "")

    '        '                                '训练区域标定位置
    '        '                                Dim TrainRaginCalib As New CogCircle
    '        '                                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(0)) / TrainRate(0)
    '        '                                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(0)) / TrainRate(0)

    '        '                                If TrainRaginPixel.Radius Then
    '        '                                    TrainRaginCalib.Radius = TrainRaginPixel.Radius / TrainRate(0)
    '        '                                    tmpPMAlign1.Pattern.TrainRegion = TrainRaginCalib
    '        '                                    tmpPMAlign1.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '        '                                    tmpPMAlign1.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '        '                                End If

    '        '                            End If
    '        '                    End Select
    '        '                End If
    '        '            End If
    '        '        Catch ex As Exception
    '        '            MsgBox("错误" & ex.Message.ToString)
    '        '        End Try
    '        'D:
    '    End Sub

    'Private Sub Button_Train1_Click_1(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        tmpPMAlign1.Pattern.Train()

    '        tmpfifo1.Run()
    '        tmpCalibCB1.Run()
    '        tmpPMAlign1.Run()

    '        CogRecordDisplay1.Image = tmpfifo1.OutputImage

    '        CogRecordDisplay1.Fit()
    '        RateZoom = 1
    '        If tmpfifo1.Operator.FrameGrabber IsNot Nothing Then
    '            CogRecordDisplay1.Record = tmpPMAlign1.CreateLastRunRecord
    '        End If
    '        If tmpPMAlign1.Results.Count > 0 Then
    '            TextBox_Score1.Text = tmpPMAlign1.Results.Item(0).Score
    '        Else
    '            TextBox_Score1.Text = ""
    '        End If
    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try


    'End Sub

    'Private Sub Button_MVcenter1_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        If Button_MVcenter1.BackColor = Color.Lime Then
    '            MoveRegion1 = False
    '            Button_MVcenter1.BackColor = Color.WhiteSmoke
    '            Button_MVcenter1.UseVisualStyleBackColor = True
    '        Else
    '            MoveRegion1 = True
    '            Button_MVcenter1.BackColor = Color.Lime
    '            Button_Move1.BackColor = Color.WhiteSmoke
    '            Button_Move1.UseVisualStyleBackColor = True
    '            Button_Pointer1.BackColor = Color.WhiteSmoke
    '            Button_Pointer1.UseVisualStyleBackColor = True
    '            GetRegion1 = False
    '        End If

    '        CogRecordDisplay1.MouseMode = CogDisplayMouseModeConstants.Pointer
    '    Catch ex As Exception

    '    End Try
    'End Sub

    '###########################################
    '获取形状终点&大小
    'Private Sub GetRegion(ByRef DisPlay As CogRecordDisplay, ByRef PMAlign As CogPMAlignTool, ByVal TYPE As String, ByRef C_X As Double, ByRef C_Y As Double, ByVal n As Integer)

    '    '终点坐标
    '    RECTX2 = (DisPlay.PointToClient(MousePosition).X / Rate0 - HomeX(n))
    '    RECTY2 = (DisPlay.PointToClient(MousePosition).Y / Rate0 - HomeY(n))
    '    Select Case TYPE
    '        Case "矩形"
    '            '图像平移量
    '            Dim pixPanX As Double = DisPlay.PanX
    '            Dim pixPanY As Double = DisPlay.PanY
    '            '转换至像素位置
    '            Dim pixelX1 As String = HomeX(n) + (RECTX1 / RateZoom) - pixPanX '329.5 + (RECTX1 / RateZoom) - pixPanX
    '            Dim pixelY1 As String = HomeY(n) + (RECTY1 / RateZoom) - pixPanY '247 + (RECTY1 / RateZoom) - pixPanY
    '            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '            Dim pixelX2 As String = HomeX(n) + (RECTX2 / RateZoom) - pixPanX
    '            Dim pixelY2 As String = HomeY(n) + (RECTY2 / RateZoom) - pixPanY
    '            '训练区域像素位置               
    '            Dim TrainRaginPixel As New CogRectangleAffine
    '            'TrainRagin.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '            TrainRaginPixel.CenterX = pixelX1 + (pixelX2 - pixelX1) / 2  'regionCenterX
    '            TrainRaginPixel.CenterY = pixelY1 + (pixelY2 - pixelY1) / 2 'regionCenterY
    '            If pixelX2 <> pixelX1 And pixelY1 <> pixelY2 Then
    '                DisPlay.StaticGraphics.Clear()
    '                TrainRaginPixel.SideXLength = Abs(pixelX2 - pixelX1)
    '                TrainRaginPixel.SideYLength = Abs(pixelY2 - pixelY1)
    '                C_X = TrainRaginPixel.SideXLength '保留X大小
    '                C_Y = TrainRaginPixel.SideYLength '保留Y大小
    '                DisPlay.StaticGraphics.Add(TrainRaginPixel, "")
    '            End If
    '            '显示中心点
    '            Dim CenterPoint As New CogPointMarker
    '            CenterPoint.SizeInScreenPixels = 10
    '            CenterPoint.X = TrainRaginPixel.CenterX
    '            CenterPoint.Y = TrainRaginPixel.CenterY
    '            DisPlay.StaticGraphics.Add(CenterPoint, "")

    '            '转训练区域标定位置
    '            Dim TrainRaginCalib As New CogRectangleAffine

    '            If trainAngle(n) > -2 And trainAngle(n) < 2 Then
    '                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '                If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
    '                    TrainRaginCalib.SideYLength = TrainRaginPixel.SideYLength / TrainRate(n)
    '                    TrainRaginCalib.SideXLength = TrainRaginPixel.SideXLength / TrainRate(n)
    '                    PMAlign.Pattern.TrainRegion = TrainRaginCalib
    '                    PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '                    PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '                End If
    '            ElseIf trainAngle(n) > 88 And trainAngle(n) < 92 Then
    '                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)

    '                If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
    '                    TrainRaginCalib.SideYLength = TrainRaginPixel.SideXLength / TrainRate(n)
    '                    TrainRaginCalib.SideXLength = TrainRaginPixel.SideYLength / TrainRate(n)
    '                    PMAlign.Pattern.TrainRegion = TrainRaginCalib
    '                    PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '                    PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '                End If
    '            ElseIf trainAngle(n) > -92 And trainAngle(n) < -88 Then
    '                TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '                If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
    '                    TrainRaginCalib.SideYLength = TrainRaginPixel.SideXLength / TrainRate(n)
    '                    TrainRaginCalib.SideXLength = TrainRaginPixel.SideYLength / TrainRate(n)
    '                    PMAlign.Pattern.TrainRegion = TrainRaginCalib
    '                    PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '                    PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '                End If
    '            ElseIf trainAngle(n) > 178 And trainAngle(n) < 182 Then
    '                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)

    '                If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
    '                    TrainRaginCalib.SideYLength = TrainRaginPixel.SideYLength / TrainRate(n)
    '                    TrainRaginCalib.SideXLength = TrainRaginPixel.SideXLength / TrainRate(n)
    '                    PMAlign.Pattern.TrainRegion = TrainRaginCalib
    '                    PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '                    PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '                End If
    '            End If


    '        Case "圆形"
    '            '图像平移量
    '            Dim pixPanX As Double = CogRecordDisplay1.PanX
    '            Dim pixPanY As Double = CogRecordDisplay1.PanY
    '            '转换至像素位置
    '            Dim pixelX1 As String = HomeX(n) + (RECTX1 / RateZoom) - pixPanX
    '            Dim pixelY1 As String = HomeY(n) + (RECTY1 / RateZoom) - pixPanY
    '            ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    '            '转换至像素位置
    '            Dim pixelX2 As String = HomeX(n) + (RECTX2 / RateZoom) - pixPanX
    '            Dim pixelY2 As String = HomeY(n) + (RECTY2 / RateZoom) - pixPanY

    '            Dim TrainRaginPixel As New CogCircle
    '            '选取点即原点
    '            TrainRaginPixel.CenterX = pixelX1
    '            TrainRaginPixel.CenterY = pixelY1
    '            If pixelX2 <> pixelX1 And pixelY1 <> pixelY2 Then
    '                DisPlay.StaticGraphics.Clear()
    '                TrainRaginPixel.Radius = Sqrt((pixelX2 - pixelX1) ^ 2 + (pixelY2 - pixelY1) ^ 2)
    '                C_X = TrainRaginPixel.Radius '保留半径大小
    '                DisPlay.StaticGraphics.Add(TrainRaginPixel, "")
    '                '显示中心点
    '                Dim CenterPoint As New CogPointMarker
    '                CenterPoint.SizeInScreenPixels = 10
    '                CenterPoint.X = TrainRaginPixel.CenterX
    '                CenterPoint.Y = TrainRaginPixel.CenterY
    '                DisPlay.StaticGraphics.Add(CenterPoint, "")

    '                'Dim LIN1 As New CogLine
    '                'LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
    '                'DisPlay.StaticGraphics.Add(LIN1, "")

    '                'Dim LIN2 As New CogLine
    '                'LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
    '                'DisPlay.StaticGraphics.Add(LIN2, "")

    '            End If

    '            '训练区域标定位置
    '            Dim TrainRaginCalib As New CogCircle
    '            If trainAngle(n) > -2 And trainAngle(n) < 2 Then
    '                TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '            ElseIf trainAngle(n) > 88 And trainAngle(n) < 92 Then
    '                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '            ElseIf trainAngle(n) > -92 And trainAngle(n) < -88 Then
    '                TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '            ElseIf trainAngle(n) > 178 And trainAngle(n) < 182 Then
    '                TrainRaginCalib.CenterY = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
    '                TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
    '            End If

    '            If TrainRaginPixel.Radius Then
    '                TrainRaginCalib.Radius = TrainRaginPixel.Radius / TrainRate(n)
    '                PMAlign.Pattern.TrainRegion = TrainRaginCalib
    '                PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
    '                PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
    '            End If
    '    End Select


    'End Sub

    Private Sub GetStartPoint(ByRef DisPlay As CogRecordDisplay, ByRef PMAlign As CogPMAlignTool, ByVal TYPE As String, ByVal C_X As Double, ByVal C_Y As Double, ByVal n As Integer)

        Select Case TYPE
            Case "矩形"

                '图像平移量
                Dim pixPanX As Double = DisPlay.PanX
                Dim pixPanY As Double = DisPlay.PanY
                '转换至像素位置
                Dim pixelX1 As String = HomeX(n) + (RECTX1 / RateZoom) - pixPanX '329.5 + (RECTX1 / RateZoom) - pixPanX
                Dim pixelY1 As String = HomeY(n) + (RECTY1 / RateZoom) - pixPanY '247 + (RECTY1 / RateZoom) - pixPanY
                ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                '训练区域像素位置               
                Dim TrainRaginPixel As New CogRectangleAffine
                'TrainRagin.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
                TrainRaginPixel.CenterX = pixelX1
                TrainRaginPixel.CenterY = pixelY1
                If RgSideX > 0 And RgSideY > 0 Then
                    DisPlay.StaticGraphics.Clear()
                    TrainRaginPixel.SideXLength = C_X
                    TrainRaginPixel.SideYLength = C_Y
                    DisPlay.StaticGraphics.Add(TrainRaginPixel, "")
                End If

                '训练区域标定位置
                Dim TrainRaginCalib As New CogRectangleAffine

                If trainAngle(n) > -2 And trainAngle(n) < 2 Then
                    TrainRaginCalib.CenterX = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                    TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                    If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
                        TrainRaginCalib.SideYLength = TrainRaginPixel.SideYLength / TrainRate(n)
                        TrainRaginCalib.SideXLength = TrainRaginPixel.SideXLength / TrainRate(n)
                        PMAlign.Pattern.TrainRegion = TrainRaginCalib
                        PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
                        PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
                    End If
                ElseIf trainAngle(n) > 88 And trainAngle(n) < 92 Then
                    TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)

                    If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
                        TrainRaginCalib.SideYLength = TrainRaginPixel.SideXLength / TrainRate(n)
                        TrainRaginCalib.SideXLength = TrainRaginPixel.SideYLength / TrainRate(n)
                        PMAlign.Pattern.TrainRegion = TrainRaginCalib
                        PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
                        PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
                    End If
                ElseIf trainAngle(n) > -92 And trainAngle(n) < -88 Then
                    TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                    If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
                        TrainRaginCalib.SideYLength = TrainRaginPixel.SideXLength / TrainRate(n)
                        TrainRaginCalib.SideXLength = TrainRaginPixel.SideYLength / TrainRate(n)
                        PMAlign.Pattern.TrainRegion = TrainRaginCalib
                        PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
                        PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
                    End If
                ElseIf trainAngle(n) > 178 And trainAngle(n) < 182 Then
                    TrainRaginCalib.CenterY = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)

                    If TrainRaginPixel.SideXLength > 0 And TrainRaginPixel.SideYLength > 0 Then
                        TrainRaginCalib.SideYLength = TrainRaginPixel.SideYLength / TrainRate(n)
                        TrainRaginCalib.SideXLength = TrainRaginPixel.SideXLength / TrainRate(n)
                        PMAlign.Pattern.TrainRegion = TrainRaginCalib
                        PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
                        PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
                    End If
                End If

            Case "圆形"

                '图像平移量
                Dim pixPanX As Double = DisPlay.PanX
                Dim pixPanY As Double = DisPlay.PanY
                '转换至像素位置
                Dim pixelX1 As String = HomeX(n) + (RECTX1 / RateZoom) - pixPanX
                Dim pixelY1 As String = HomeY(n) + (RECTY1 / RateZoom) - pixPanY
                '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

                Dim TrainRaginPixel As New CogCircle
                '选取点即原点
                TrainRaginPixel.CenterX = pixelX1
                TrainRaginPixel.CenterY = pixelY1
                If C_X > 0 Then
                    DisPlay.StaticGraphics.Clear()
                    TrainRaginPixel.Radius = C_X
                    DisPlay.StaticGraphics.Add(TrainRaginPixel, "")

                    Dim CenterPoint As New CogPointMarker
                    CenterPoint.SizeInScreenPixels = 10
                    CenterPoint.X = TrainRaginPixel.CenterX
                    CenterPoint.Y = TrainRaginPixel.CenterY
                    DisPlay.StaticGraphics.Add(CenterPoint, "")

                    'Dim LIN1 As New CogLine
                    'LIN1.SetFromStartXYEndXY(329.5, 0, 329.5, 247)
                    'DisPlay.StaticGraphics.Add(LIN1, "")

                    'Dim LIN2 As New CogLine
                    'LIN2.SetFromStartXYEndXY(0, 247, 329.5, 247)
                    'DisPlay.StaticGraphics.Add(LIN2, "")
                End If


                '训练区域标定位置
                Dim TrainRaginCalib As New CogCircle
                If trainAngle(n) > -2 And trainAngle(n) < 2 Then
                    TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                ElseIf trainAngle(n) > 88 And trainAngle(n) < 92 Then
                    TrainRaginCalib.CenterY = (TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                ElseIf trainAngle(n) > -92 And trainAngle(n) < -88 Then
                    TrainRaginCalib.CenterY = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                ElseIf trainAngle(n) > 178 And trainAngle(n) < 182 Then
                    TrainRaginCalib.CenterY = (TrainRaginPixel.CenterY - HomeY(n)) / TrainRate(n)
                    TrainRaginCalib.CenterX = -(TrainRaginPixel.CenterX - HomeX(n)) / TrainRate(n)
                End If
                If TrainRaginPixel.Radius Then
                    TrainRaginCalib.Radius = TrainRaginPixel.Radius / TrainRate(n)
                    PMAlign.Pattern.TrainRegion = TrainRaginCalib
                    PMAlign.Pattern.Origin.TranslationX = TrainRaginCalib.CenterX
                    PMAlign.Pattern.Origin.TranslationY = TrainRaginCalib.CenterY
                End If
        End Select

    End Sub


    Private Sub Button_FifoSet2_Click(ByVal sender As Object, ByVal e As EventArgs)
        Try
            FrmFifoTool.CogAcqFifoEditV21.Subject = tmpfifo2
            FrmFifoTool.ShowDialog()

        Catch ex As Exception
            MsgBox("错误：" & ex.Message.ToString)
        End Try
    End Sub

    'Private Sub CogRecordDisplay2_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    '    Try

    '        RECTX1 = (CogRecordDisplay2.PointToClient(MousePosition).X / Rate0 - HomeX(0))
    '        RECTY1 = (CogRecordDisplay2.PointToClient(MousePosition).Y / Rate0 - HomeY(0))



    '    Catch ex As Exception

    '    End Try
    'End Sub

    '    Private Sub CogRecordDisplay2_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    '        '获取收搜区域终点
    '        If GetRegion2 = False Then
    '            GoTo D
    '        End If
    '        Try
    '            If e.Button = Windows.Forms.MouseButtons.Left Then
    '                If ComboBox_Region2.SelectedIndex <> -1 Then '有选择
    '                    Dim str As String = ComboBox_Region2.SelectedItem.ToString
    '                    'GetRegion(CogRecordDisplay2, tmpPMAlign2, str, RgSideX, RgSideY, 1)
    '                End If
    '                ' 
    '            End If
    '        Catch ex As Exception

    '        End Try
    'D:

    '        '移动搜索区域

    '        If MoveRegion2 = False Then
    '            GoTo E
    '        End If
    '        RECTX1 = (CogRecordDisplay2.PointToClient(MousePosition).X / Rate0 - HomeX(0))
    '        RECTY1 = (CogRecordDisplay2.PointToClient(MousePosition).Y / Rate0 - HomeY(0))

    '        If e.Button = Windows.Forms.MouseButtons.Left Then
    '            If ComboBox_Region2.SelectedIndex <> -1 Then '有选择
    '                Dim str As String = ComboBox_Region2.SelectedItem.ToString
    '                GetStartPoint(CogRecordDisplay2, tmpPMAlign2, str, RgSideX, RgSideY, 1)
    '            End If
    '        End If

    'E:
    '    End Sub

    'Private Sub Button_ZoomIn2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        RateZoom = RateZoom * 0.5
    '        CogRecordDisplay2.Zoom = 0.5 * CogRecordDisplay2.Zoom
    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_ZoomOut2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        RateZoom = RateZoom * 2
    '        CogRecordDisplay2.Zoom = 2 * CogRecordDisplay2.Zoom
    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_Move2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        CogRecordDisplay2.MouseMode = CogDisplayMouseModeConstants.Pan
    '        If Button_Move2.BackColor = Color.Lime Then
    '            Button_Move2.BackColor = Color.WhiteSmoke
    '            Button_Move2.UseVisualStyleBackColor = True
    '        Else
    '            Button_Move2.BackColor = Color.Lime
    '            Button_Pointer2.BackColor = Color.WhiteSmoke
    '            Button_Pointer2.UseVisualStyleBackColor = True
    '            GetRegion2 = False
    '            Button_MVcenter2.BackColor = Color.WhiteSmoke
    '            Button_MVcenter2.UseVisualStyleBackColor = True
    '            MoveRegion2 = False
    '        End If

    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_Pointer2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        If GetRegion2 = True Then
    '            GetRegion2 = False
    '            Button_Pointer2.BackColor = Color.WhiteSmoke
    '            Button_Pointer2.UseVisualStyleBackColor = True
    '        Else
    '            GetRegion2 = True
    '            Button_Pointer2.BackColor = Color.Lime
    '            Button_Move2.BackColor = Color.WhiteSmoke
    '            Button_Move2.UseVisualStyleBackColor = True
    '            Button_MVcenter2.BackColor = Color.WhiteSmoke
    '            Button_MVcenter2.UseVisualStyleBackColor = True
    '            MoveRegion2 = False
    '        End If

    '        tmpfifo2.Run() '相机运行
    '        tmpCalibCB2.Run() '标定工具运行
    '        tmpPMAlign2.InputImage = tmpCalibCB2.OutputImage
    '        tmpPMAlign2.Pattern.TrainImage = tmpCalibCB2.OutputImage

    '        CogRecordDisplay2.MouseMode = CogDisplayMouseModeConstants.Pointer

    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_MVcenter2_Click(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        If Button_MVcenter2.BackColor = Color.Lime Then
    '            MoveRegion2 = False
    '            Button_MVcenter2.BackColor = Color.WhiteSmoke
    '            Button_MVcenter2.UseVisualStyleBackColor = True
    '        Else
    '            MoveRegion2 = True
    '            Button_MVcenter2.BackColor = Color.Lime
    '            Button_Move2.BackColor = Color.WhiteSmoke
    '            Button_Move2.UseVisualStyleBackColor = True
    '            Button_Pointer2.BackColor = Color.WhiteSmoke
    '            Button_Pointer2.UseVisualStyleBackColor = True
    '            GetRegion2 = False
    '        End If

    '        CogRecordDisplay2.MouseMode = CogDisplayMouseModeConstants.Pointer
    '    Catch ex As Exception

    '    End Try
    'End Sub

    'Private Sub Button_Train2_Click_1(ByVal sender As Object, ByVal e As EventArgs)
    '    Try
    '        tmpPMAlign2.Pattern.Train()

    '        tmpfifo2.Run()
    '        tmpCalibCB2.Run()
    '        tmpPMAlign2.Run()

    '        CogRecordDisplay2.Image = tmpfifo2.OutputImage

    '        CogRecordDisplay2.Fit()
    '        RateZoom = 1
    '        If tmpfifo2.Operator.FrameGrabber IsNot Nothing Then
    '            CogRecordDisplay2.Record = tmpPMAlign2.CreateLastRunRecord
    '        End If
    '        If tmpPMAlign2.Results.Count > 0 Then
    '            TextBox_Score2.Text = tmpPMAlign2.Results.Item(0).Score
    '        Else
    '            TextBox_Score2.Text = ""
    '        End If
    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try

    'End Sub


    'Private Sub Button_Fit2_Click(sender As Object, e As EventArgs)
    '    Try
    '        CogRecordDisplay2.Fit()
    '        RateZoom = 1
    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        Dim path As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\"
    '        If Dir(path, FileAttribute.Directory) = "" Then '如果路径不存在,则创建路径
    '            MsgBox("路径不存在:" & path)
    '        Else
    '            CogSerializer.SaveObjectToFile(gTBCheck(2), path & "TBCheck3.vpp")
    '            MsgBox("模板3保存成功！")
    '            TeachPhotoPoint(3)
    '            Save_Fhoto3()
    '            Button_NextStep.Enabled = True
    '        End If
    '    Catch ex As Exception
    '        MsgBox("保存失败！错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_TakePhoto3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_TakePhoto3.Click
    '    CircularPhotography(3)
    'End Sub

    'Private Sub Button_ExpoureOK3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try

    '        tmpfifo3.Operator.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure3.Text)
    '        myAcqFifo.OwnedExposureParams.Exposure = CDbl(TextBox_Exposure3.Text)

    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_FifoSet3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        FrmFifoTool.CogAcqFifoEditV21.Subject = tmpfifo3
    '        FrmFifoTool.ShowDialog()

    '    Catch ex As Exception
    '        MsgBox("错误：" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_HighTrain3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        TBcheck_NO = 3
    '        CogRecordDisplay2.StopLiveDisplay()
    '        Button_TakePhoto3.Text = "连续取像"
    '        Button_TakePhoto3.BackColor = Color.WhiteSmoke
    '        Button_TakePhoto3.UseVisualStyleBackColor = True
    '        tmpfifo3.Run()
    '        tmpCalibCB3.Run()
    '        tmpPMAlign3.InputImage = tmpCalibCB3.OutputImage
    '        tmpPMAlign3.Pattern.TrainImage = tmpCalibCB3.OutputImage
    '        FrmPMAlignTool.CogPMAlignEditV21.Subject = tmpPMAlign3

    '        FrmPMAlignTool.ShowDialog()
    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub

    'Private Sub Button_Train3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Try
    '        tmpPMAlign3.Pattern.Train()

    '        tmpfifo3.Run()
    '        tmpCalibCB3.Run()
    '        tmpPMAlign3.Run()

    '        CogRecordDisplay3.Image = tmpfifo3.OutputImage

    '        CogRecordDisplay3.Fit()
    '        RateZoom = 1
    '        If tmpfifo3.Operator.FrameGrabber IsNot Nothing Then
    '            CogRecordDisplay3.Record = tmpPMAlign3.CreateLastRunRecord
    '        End If
    '        If tmpPMAlign3.Results.Count > 0 Then
    '            TextBox_Score3.Text = tmpPMAlign3.Results.Item(0).Score
    '        Else
    '            TextBox_Score3.Text = ""
    '        End If
    '    Catch ex As Exception
    '        MsgBox("错误:" & ex.Message.ToString)
    '    End Try
    'End Sub
    Private Sub Button_VisionMark1_Click(sender As Object, e As EventArgs) Handles Button_VisionMark1.Click
        'CogVisionControl1.SetTBCheck(gTBCheck(0), "CogCalibCheckerboardTool1", "CogPMAlignTool1")
        CogVisionControl1.SetTBCheck(gTBCheck(0), "CogCalibNPointToNPointTool1", "CogPMAlignTool1")
    End Sub

    Private Sub Button_SaveTool1_Click(sender As Object, e As EventArgs) Handles Button_SaveTool1.Click
        gTBCheck(0) = CogVisionControl1.GetTBCheck()
        Dim path As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\TBCheck1.vpp"
        CogVisionControl1.SaveTBCheck(path)
        flag(5) = True       

        'Dim Re_date As String
        'StepNO = "STEP1"
        'Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
        'Re_date = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")
        ' Re_date = SendDataAndRecvice(StepNO, "192.168.1.120", "2016")

        Dim ss(4) As String
        ss(1) = CStr(TextBox_PX.Text)
        ss(2) = CStr(TextBox_PY.Text)
        ss(3) = CStr(TextBox_PZ.Text)
        ss(4) = CStr(TextBox_PC.Text)
        Dim sql1 As String = ""
        sql1 = " update " & " photo_point " & "set x=" & ss(1) & ",y=" & ss(2) & ",z=" & ss(3) & ",c=" & ss(4) & " where PointName=" & "'photo1robot' and Product='" & TextBox_NewProdName.Text & "'"
        HandleMysql(sql1)
        'sql1 = " update " & " domino " & "set x=" & ss(5) & ",y=" & ss(6) & ",z=" & ss(7) & ",c=" & ss(8) & " where PointName=" & "'photo2robot'"
        'HandleDB(sql1, cnStr)
        MsgBox("保存拍照点1")
    End Sub

    Private Sub Button_VisionMark2_Click(sender As Object, e As EventArgs) Handles Button_VisionMark2.Click
        CogVisionControl2.SetTBCheck(gTBCheck(1), "CogCalibNPointToNPointTool1", "CogPMAlignTool1")
    End Sub

    Private Sub Button_SaveTool2_Click_1(sender As Object, e As EventArgs) Handles Button_SaveTool2.Click
        gTBCheck(1) = CogVisionControl2.GetTBCheck()
        Dim path As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\TBCheck2.vpp"
        CogVisionControl2.SaveTBCheck(path)
        flag(6) = True

        'Dim Re_date As String
        'StepNO = "STEP2"
        'Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
        'Re_date = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")
        'Re_date = SendDataAndRecvice(StepNO, "192.168.1.120", "2016")

        Dim ss(4) As String
        ss(1) = CStr(TextBox_PX.Text)
        ss(2) = CStr(TextBox_PY.Text)
        ss(3) = CStr(TextBox_PZ.Text)
        ss(4) = CStr(TextBox_PC.Text)
        Dim sql1 As String = ""
        sql1 = " update " & " photo_point " & "set x=" & ss(1) & ",y=" & ss(2) & ",z=" & ss(3) & ",c=" & ss(4) & " where PointName=" & "'photo2robot' and Product='" & TextBox_NewProdName.Text & "'"
        HandleMysql(sql1)

        MsgBox("保存拍照点2")
    End Sub

    Private Sub Button_VisionMark3_Click(sender As Object, e As EventArgs) Handles Button_VisionMark3.Click
        CogVisionControl3.SetTBCheck(gTBCheck(2), "CogCalibNPointToNPointTool1", "CogPMAlignTool1")
    End Sub

    Private Sub Button_SaveTool3_Click(sender As Object, e As EventArgs) Handles Button_SaveTool3.Click
        gTBCheck(2) = CogVisionControl3.GetTBCheck()
        Dim path As String = DebugPath & "\Tool\" & TextBox_NewProdName.Text & "\TBCheck3.vpp"
        CogVisionControl3.SaveTBCheck(path)
        flag(7) = True

        'Dim Re_date As String
        'StepNO = "STEP3"
        'Globalfunc.SendDataNORePly(StepNO, "192.168.1.120", "2016", "UTF8")
        'Re_date = Globalfunc.SendData("192.168.1.120", "2016", StepNO, "UTF8", "ASCII")
        'Re_date = SendDataAndRecvice(StepNO, "192.168.1.120", "2016")
        Save_Fhoto3()
        MsgBox("保存拍照点3")
    End Sub
    Private Sub Save_Fhoto3()
        Try
            AutoAddPhotoPoint(3).X = CStr(TextBox_PX.Text)
            AutoAddPhotoPoint(3).Y = CStr(TextBox_PY.Text)
            AutoAddPhotoPoint(3).Z = CStr(TextBox_PZ.Text)
            AutoAddPhotoPoint(3).C = CStr(TextBox_PC.Text)
            Dim sql1 As String = ""
            sql1 = " update " & " photo_point " & "set x=" & AutoAddPhotoPoint(3).X & ",y=" & AutoAddPhotoPoint(3).Y & ",z=" & AutoAddPhotoPoint(3).Z & ",c=" & AutoAddPhotoPoint(3).C & " where PointName=" & "'photo3robot' and Product='" & TextBox_NewProdName.Text & "'"
            HandleMysql(sql1)

        Catch ex As Exception
            MsgBox("Save_Fhoto3" & ex.Message.ToString)
        End Try
    End Sub
#Region "机器人移动按钮2"
    Private Sub Button_AddX_Click(sender As Object, e As EventArgs) Handles Button_AddX.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "X" + "," + CStr(ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_DecX_Click(sender As Object, e As EventArgs) Handles Button_DecX.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "X" + "," + CStr(-ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try

    End Sub

    Private Sub Button_AddY_Click(sender As Object, e As EventArgs) Handles Button_AddY.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "Y" + "," + CStr(ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub
    Private Sub Button_DecY_Click(sender As Object, e As EventArgs) Handles Button_DecY.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "Y" + "," + CStr(-ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub
    Private Sub Button_AddZ_Click(sender As Object, e As EventArgs) Handles Button_AddZ.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "Z" + "," + CStr(ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_DecZ_Click(sender As Object, e As EventArgs) Handles Button_DecZ.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "Z" + "," + CStr(-ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_DecC_Click(sender As Object, e As EventArgs) Handles Button_DecC.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "C" + "," + CStr(ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub

    Private Sub Button_AddC_Click(sender As Object, e As EventArgs) Handles Button_AddC.Click
        Try
            If Motor_flag = False Then
                MsgBox("机器人伺服OFF,请先打开先打开机器人伺服")
                Return
            End If

            Dim MoveDate As String
            Dim Revedates As String
            MoveDate = "C" + "," + CStr(-ComboBox_stepType.SelectedItem) + "," + CStr(ComboBox_speed.SelectedItem) + "," + CStr(ComboBox_Tool.SelectedItem) + vbCrLf
            Revedates = Globalfunc.SendData("192.168.1.120", "2016", MoveDate, "UTF8", "ASCII")
            'ButtonUP(1, 0)

            Dim recdata() As String = Revedates.Split(",")
            TextBox_PX.Text = Val(recdata(0))
            TextBox_PY.Text = Val(recdata(1))
            TextBox_PZ.Text = Val(recdata(2))
            TextBox_PC.Text = Val(recdata(3))
        Catch ex As Exception
            MsgBox(ex.StackTrace.ToString & vbCrLf & ex.Message.ToString)
        End Try
    End Sub
#End Region
End Class